jonniejoejonson Posted June 17, 2009 Share Posted June 17, 2009 I have 2 tables: images_table imageId : imageFilename : dateCreated videos_table videoId : videoFilename : dateCreated I want to select the id's of the 10 most recent videos or images. Therefore i need to somehow join the 2 tables and order them by dateCreated ASC and LIMIT 0,10. any ideas thanks Jon. Quote Link to comment https://forums.phpfreaks.com/topic/162616-solved-please-how-can-you-do-this/ Share on other sites More sharing options...
jonniejoejonson Posted June 17, 2009 Author Share Posted June 17, 2009 $sql = "(SELECT a.imageId, a.dateCreated AS orderDate FROM library_images AS a WHERE a.userId='$userId') UNION (SELECT b.videoId, b.dateCreated AS orderDate FROM library_videos AS b WHERE b.userId='$userId') ORDER BY orderDate DESC LIMIT 0,10"; Quote Link to comment https://forums.phpfreaks.com/topic/162616-solved-please-how-can-you-do-this/#findComment-858295 Share on other sites More sharing options...
kickstart Posted June 17, 2009 Share Posted June 17, 2009 Hi That is pretty much what I was about to put, just with a field to say whether it was an image or video. SELECT * FROM (SELECT 'Image' AS Type, imageId AS Id, imageFileName AS FileName, dateCreated FROM images_table UNION SELECT 'Video' AS Type, videoId AS Id, videoFilename AS FileName, dateCreated FROM videos_table) Deriv1 ORDER BY dateCreated DESC LIMIT 0,10 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/162616-solved-please-how-can-you-do-this/#findComment-858299 Share on other sites More sharing options...
jonniejoejonson Posted June 17, 2009 Author Share Posted June 17, 2009 thanks Keith. Quote Link to comment https://forums.phpfreaks.com/topic/162616-solved-please-how-can-you-do-this/#findComment-858306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.