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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Welling Posted September 5, 2007 Author Share Posted September 5, 2007 Do you understand? Thanks Quote Link to comment 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 ) Quote Link to comment 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!!!!!!!!!!! Quote Link to comment 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. Quote Link to comment 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.