Page 1 of 1
Need to combine queries, but unsure where to even start..
Posted: Fri Oct 07, 2005 8:49 pm
by Skara
Alright, here's the one query:
Code: Select all
SELECT id,file,name,info,uploaded FROM photos ORDER BY uploaded DESC LIMIT 10;
and here's the second:
Code: Select all
SELECT time,post FROM blog ORDER BY time DESC LIMIT 10;
I need the latest 10 entries of both combined. ...er.. In other words, I need the latest 10 entries as if they were in one table.
I know somehow I need a JOIN, but I don't see how to combine it with the two ORDER BYs and get the right results. ^^;
Posted: Sat Oct 08, 2005 12:52 am
by elecktricity
im not sure, try displaying 5 of each of them displaying the results, but that wouldnt put them side by side, you may want to try this out:
Code: Select all
<?PHP
SELECT * FROM content, users DESC LIMIT 10
?>
Posted: Sat Oct 08, 2005 2:05 am
by n00b Saibot
Code: Select all
SELECT p.id, p.file, p.name, p.info, p.uploaded, b.time, b.post
FROM photos AS p, blog as b
ORDER BY uploaded DESC, time DESC
LIMIT 10
Posted: Sat Oct 08, 2005 6:57 am
by feyd
I don't think either of the solutions posted previously will cut it, but a UNION will
Code: Select all
(SELECT `id`,`file`,`name`,`info`,`uploaded` FROM `photos`)
UNION
(SELECT '','','',`post`,`time` FROM `blog`)
ORDER BY `uploaded` DESC LIMIT 10
Posted: Sat Oct 08, 2005 7:07 am
by n00b Saibot
feyd wrote:I don't think either of the solutions posted previously will cut it
can you explain plz
Posted: Sat Oct 08, 2005 7:15 am
by feyd
both are joining tables that have no relation to each other therefore building an O^n result set. Sorting based on one field first gives priority to that field. The fields need to be processed as one, only a union can do that.
Posted: Sat Oct 08, 2005 7:22 am
by n00b Saibot
seriously, I first did think about using a UNION but, well... umm. OK thanks for explaining

Posted: Sun Oct 09, 2005 5:57 pm
by Skara
Voila! Thanks, feyd.
