Welling Posted August 27, 2007 Share Posted August 27, 2007 I'm making a fotolog for me and my friends and I don't know how to do the query for the friends of each fotolog... The SQL Tables are: CREATE TABLE `friends` ( `id` smallint(20) NOT NULL auto_increment, `id_flog` smallint(20) NOT NULL default '0', `id_friend` smallint(20) NOT NULL default '0', ) CREATE TABLE `imgs` ( `id` smallint(20) NOT NULL auto_increment, `id_flog` smallint(20) NOT NULL default '0', `src` smallint(20) NOT NULL default '0', `date` int(10) NOT NULL default '0', ...... ) The table friends has a row for each friend of id_flog and the table imgs the images of fotolog in id_flog Now, I had to get the friends of id_flog='2' and order by the date of their last image in imgs The query for friends table is something like: SELECT * FROM friends WHERE id_flog='2' but now, there're to merge with the query for imgs, where friends.id_friend=imgs.id_flog and from imgs only need the last image from each fotolog and from friends the 5 friends that post recently an image order DESC. The same of the Fotologs of www.fotolog.com Thanks Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/ Share on other sites More sharing options...
fenway Posted August 27, 2007 Share Posted August 27, 2007 You'll need to pull each "group" of images separately, then UNION them. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335228 Share on other sites More sharing options...
Welling Posted August 27, 2007 Author Share Posted August 27, 2007 Ok, thanks, but I don't know how to do it... can you explain me? Thanks, Welling Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335239 Share on other sites More sharing options...
fenway Posted August 27, 2007 Share Posted August 27, 2007 You need the last 5 images from each friend? Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335275 Share on other sites More sharing options...
Welling Posted August 27, 2007 Author Share Posted August 27, 2007 No, no, I need 5 friends and order them with the date of their last image. Thanks. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335466 Share on other sites More sharing options...
fenway Posted August 27, 2007 Share Posted August 27, 2007 OK... well, you can join to get the 5 friends, then use this as a derived table, and grab the latest image for each. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335494 Share on other sites More sharing options...
Welling Posted August 27, 2007 Author Share Posted August 27, 2007 I've read about derived tables now but I don't know how to do the query yet. Can you give me a example of the query I need with the two tables? Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335519 Share on other sites More sharing options...
fenway Posted August 27, 2007 Share Posted August 27, 2007 I'm confused about id_flog vs id_friednd. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335625 Share on other sites More sharing options...
Welling Posted August 27, 2007 Author Share Posted August 27, 2007 Each fotolog has a id. id_flog contains the id of x fotolog and id_friend the id of one of his friends Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-335648 Share on other sites More sharing options...
fenway Posted August 28, 2007 Share Posted August 28, 2007 So images are linked to fotologs, and not friends? How do you know which friend has which image? are friends and fotologs 1-to-1? Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-336114 Share on other sites More sharing options...
Welling Posted August 28, 2007 Author Share Posted August 28, 2007 Yes, images are linked to fotologs. The friends are also fotologs. Each fotolog can add other fotologs as friends. Like fotolog.com. Do you understand? Thanks. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-336139 Share on other sites More sharing options...
fenway Posted August 28, 2007 Share Posted August 28, 2007 I have no idea what fotolog.com is. So the friends table is many-to-many? I just don't understand why you're linking friend_id with flog_id. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-336578 Share on other sites More sharing options...
Welling Posted August 29, 2007 Author Share Posted August 29, 2007 I have a table that contains all photo-blogs (Flogs or Fotologs) and each flog has a ID, what is used in friend_id and flog_id. The flogs can have friends and the ids of the flogs friends are on the table friends, For example, if flog with id 2 has the flogs 1 and 8, the table friends contains this: INSERT INTO `friends` VALUES (1, 2, 1); INSERT INTO `friends` VALUES (2, 2, ; And the friends of flog 2 are: SELECT id_friend FROM friends WHERE flog_id='2' Photos of flog 2 are SELECT * FROM imgs WHERE flog_id='2' What I have to do is select all the friends, the last photo of each friend, order the friends by their last photo date (DESC) and only show 5 friends. Do you understand now? Million thanks! P.D.:Fotolog.com is a site that host photo-blogs. Example: http://www.fotolog.com/fotolog Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-337209 Share on other sites More sharing options...
Welling Posted September 5, 2007 Author Share Posted September 5, 2007 Do you understand? Thanks Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-342280 Share on other sites More sharing options...
fenway Posted September 7, 2007 Share Posted September 7, 2007 This should get you the most recent img for each friend's flog (untested): SELECT * from imgs WHERE ( id_flog, `date` ) = ( SELECT id_flog, MAX(`date`) AS recent FROM imgs WHERE id_flog IN ( SELECT id_friend FROM friends WHERE flog_id='2' ) GROUP by id_flog ) Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-343707 Share on other sites More sharing options...
Welling Posted September 7, 2007 Author Share Posted September 7, 2007 Changing the = to IN it works, because with the = only works with 1 friend. The key to get the select is MAX() and the subqueries. Every day I learn a new thing about MySQL... A Million Thanks To you!!!!!!!!!!! Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-343872 Share on other sites More sharing options...
fenway Posted September 8, 2007 Share Posted September 8, 2007 Changing the = to IN it works, because with the = only works with 1 friend. The key to get the select is MAX() and the subqueries. Every day I learn a new thing about MySQL... A Million Thanks To you!!!!!!!!!!! Oops... my bad... glad you got it working. Link to comment https://forums.phpfreaks.com/topic/66874-solved-help-for-friends-fotolog-column/#findComment-344457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.