1

hye guys i'v a problem with selecting a date from another table. basically i have tables like

 1) users table, 
 2)follow table, 
 3) photos table, etc. 

i'm trying to select the date from photos , but its not working. Here is my mysql query. Here I'm selecting the followers from the follow table where the follower = the user_id

followers_photo.php

$the_user = mysql_query("SELECT * FROM users WHERE username = '$getuser'"); 
$tuser=mysql_fetch_array($the_user); 
$isuser = mysql_num_rows($the_user); 

$tu_id = $tuser['id']; 
////users whos following Me 
$followers = mysql_query("SELECT * FROM follow WHERE followers ='$tu_id' order by id desc"); 
$f_s_count = mysql_num_rows($followers); 

and here I'm retrieving the photos base on their date and the following id.

index.php

include('func/followers_photos.php');
       if($f_s_count > '0')
           { 
        while($uim = mysql_fetch_assoc($followers)){ 
        $i_id = $uim['following']; 
           $followers_image = mysql_query("SELECT * FROM photos WHERE user_id = '$i_id' order by date"); 
        $disimg = $fimage['img'];
        $disid = $fimage['id'];
        $distime = $fimage['date'];
        $i_like=$fimage['likes'];
        //$i_unlike=$fimage['down'];
        $likes_ip = mysql_query("SELECT * FROM voting_ip  WHERE mes_id_fk = '$disid'");
        $ipadd = mysql_fetch_assoc($likes_ip);
        $check_likes= $ipadd['mes_id_fk'];
        $user_follow_details = mysql_query("SELECT * FROM users WHERE id = '$i_id'");
        $ufde = mysql_fetch_assoc($user_follow_details);
        $uiname = $ufde['username'];
        $uinim = $ufde['img'];
        $ip=$_SERVER['REMOTE_ADDR']; 

ending brackets are after the html code.

above code works but i can't manage to sort the photos base on their date because i'm selecting data from the table base on the id.

here is what i tried so far.

$the_user = mysql_query("SELECT * FROM users WHERE username = '$getuser'"); 
$tuser=mysql_fetch_array($the_user); 
$isuser = mysql_num_rows($the_user); 

$tu_id = $tuser['id']; 
////users whos following Me 
$followers = mysql_query("SELECT * FROM follow,photos WHERE followers = photos.user_id order by photos.date desc"); 
$f_s_count = mysql_num_rows($followers); 

above code will work but will select all the photos from the database and not from the the following id. what i mean is even the user i don't follow their picture will show.

what i'm trying to accomplish is select from who i follow and sort their photos by the photo date which is stored in photos table. i'm really stuck , anyone could help me out ? your replies are appreciated. sorry for my bad english.

EDIT

now this works as i want but it shows the images multiple times.

$followers = mysql_query("SELECT * FROM follow,photos WHERE photos.user_id = '$tu_id' and follow.followers = photos.user_id order by photos.date desc");

Another Edit

now with this it doesn't display the images multiple times but whenever i post a new image it wont be at the top its still sort them base on the ID. just a little modification on @GonGordon Linoff suggestion

SELECT distinct follow.*
FROM photos join
     follow
     on  follow.followers = photos.user_id
WHERE    photos.user_id = $tu_id
order by photos.date desc;
4

2 回答 2

0

你尝试过这样的事情吗?

$followers = mysql_query("SELECT * FROM follow, photos 
WHERE follow.followers = photos.user_id and photos.user_id = '$i_id' 
order by photos.date desc"); 
于 2013-08-07T12:14:08.587 回答
0

您的最终查询是在正确的轨道上。您只需要添加用户的条件:

SELECT * 
FROM follow join
     photos
     on follow.followers = photos.user_id
WHERE photos.user_id = $the_user
order by photos.date desc;

编辑:

如果您想获取给定用户关注的所有照片,那么我倒退了逻辑:

SELECT distinct photos.*
FROM follow join
     photos
     on follow.followers = photos.user_id
WHERE follow.user_id = $the_user
order by photos.date desc;
于 2013-08-07T12:14:16.460 回答