简体   繁体   中英

Mysql select date from different table

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;

Did you try something like this?

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

Your final query is on the right track. You just need to add in the condition on which user:

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

EDIT:

If you want to get all photos that a given user is following, then I did the logic backwards:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM