Jump to content

[SOLVED] Help for friends fotolog column


Welling

Recommended Posts

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

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

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 )

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.