Mysql从不同的表中选择日期

时间:2013-08-07 12:07:10

标签: php mysql

hye伙计们,我从另一张桌子上选择一个日期是个问题。 基本上我有像

这样的表格
 1) users table, 
 2)follow table, 
 3) photos table, etc. 

我正在尝试从照片中选择日期,但它不起作用。 这是我的mysql查询。在这里,我从下表中选择关注者,其中关注者= 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); 

在这里我根据日期和以下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']; 

结束括号位于html代码之后。

上面的代码工作,但我无法根据他们的日期对照片进行排序,因为我正在从基于id的表格中选择数据。

这是我到目前为止所尝试的内容。

$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); 

上面的代码可以使用,但会从数据库中选择所有照片,而不是从以下ID中选择。我的意思是即使是我不遵循他们的照片的用户也会显示。

我想要完成的是从我关注的人中选择并按照存储在照片表中的照片日期对照片进行排序。我真的被卡住了,有人可以帮帮我吗?您的回复表示赞赏。抱歉我的英语不好。

修改

现在这可以按照我想要的方式工作,但它会多次显示图像。

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

另一个编辑

现在有了它,它不会多次显示图像,但每当我发布一个新图像时,它不会在顶部,它仍然根据ID排序。 只是对@GonGordon Linoff建议的一点修改

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

2 个答案:

答案 0 :(得分: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"); 

答案 1 :(得分: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;