December 20th, 2012, 09:35 PM
Select all rows and select just first row?
Currently do a select to produce rows of posts for a specific topic:
$queryposts = $db->query("SELECT p.pid, p.author, p.message, p.subject, p.dateline, m.username FROM posts p INNER JOIN members m ON m.username = p.author WHERE.... ORDER BY p.pid ASC");
I would like to ALSO select the FIRST post row for the same topic, assign it to a variable. I could do this via:
$firstpostquery = $db->query("SELECT pid FROM posts WHERE... ORDER BY dateline LIMIT 1");
$isthefirstpost = $db->fetch_array($firstpostquery);
However the second select seems inefficient because I'm already grabbing all rows in the first select and the second select has to go through the entire posts table again.
Is there a way to use the first select and also do a sub select for also obtaining the first row - or some other method that is more efficient?
December 20th, 2012, 10:11 PM
since you're retrieving all the rows that meet your criteria, the function to extract the first row's data should be performed in your application language
that's not only the easiest but also the most efficient
December 21st, 2012, 06:50 AM
Originally Posted by r937
Thanks Rudy. Great advice. In case anyone else is looking for a similar solution, a good example can be found here: