1

我真的需要你的帮助。请帮我解决这个问题。

我有这两张表,relation它们之间是user_id. 我完成了选择我关注的人并将他们的图像显示在我的墙上。但是,我有一个奇怪的问题:照片按DESC我想要的方式排序,但它们是按user_id. 这意味着无论我先关注谁,他们的图像都会首先按DESC.

我尝试了几乎所有可能的方法,以根据最新照片作为顶部对照片进行排序DESC,但我做不到。这是我的表格,我将向您展示我尝试过的所有可能的事情:

CREATE TABLE IF NOT EXISTS `photos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `img` varchar(255) NOT NULL,
  `about` text NOT NULL,
  `date` varchar(222) NOT NULL,
  `user_id` varchar(255) NOT NULL,
  `likes` int(255) NOT NULL,
  `down` int(255) NOT NULL,
  PRIMARY KEY (`id`)
) ;

CREATE TABLE IF NOT EXISTS `follow` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `followers` varchar(255) NOT NULL,
  `following` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
}

Followers_photo.php,我following id从中检索:

$the_user = mysql_query("SELECT * FROM users WHERE username = '$getuser'");
$tuser=mysql_fetch_array($the_user);
$tu_id = $tuser['id'];

$followers = mysql_query("SELECT distinct follow.*    
 FROM follow JOIN photos ON (follow.followers  = $tu_id)
order by photos.date DESC");
$f_s_count = mysql_num_rows($followers);    
  • index.php 我在这里显示图像。

while($uim = mysql_fetch_assoc($followers)){ $i_id = $uim['following']; $followers_image = mysql_query("SELECT distinct photos.* FROM photos JOIN follow ON (photos.user_id = $i_id) GROUP BY RAND() order by date DESC");


以上是有效的,但正如我所提到的,它根据日期DESCuser_id我不想要的图像对图像进行排序。我希望它停止根据user_id

追随者照片.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'];
$tu_name =$tuser ['username'];
////users whos following Me
$followers = mysql_query(" SELECT distinct follow.* FROM photos join  follow on  follow.followers = photos.user_id WHERE follow.followers = $tu_id order by photos.date DESC");
//
$f_s_count = mysql_num_rows($followers);

索引.php

    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 DESC ");

以上与第一步相同。谁能指出我正确的方法?根据我关注的人对照片进行排序date DESC,最后发布的人优先。谢谢各位,非常感谢你们的帮助。对不起我的英语不好。

更新

现在有了 vegatripy 提供的解决方案,图像是重复的,而不是按需要排序。左边是现在显示的内容。我想要的是排序为正确的图像。有任何想法吗? http://i.stack.imgur.com/Cod6z.jpg

解决了

    $followers = mysql_query("SELECT PHOTOs.IMG, follow.followers, follow.following, photos.date,photos.img,photos.likes,photos.down,photos.id,photos.about
FROM FOLLOW
JOIN PHOTOs ON ( PHOTOs.USER_ID = FOLLOW.FOLLOWING ) 
WHERE FOLLOW.FOLLOWERS =31
ORDER BY PHOTOs.DATE DESC");
$f_s_count = mysql_num_rows($followers);
while($fimage = mysql_fetch_assoc($followers)){
$i_id = $fimage['following'];
$disimg = $fimage['img'];
$disid = $fimage['id'];
$distime = $fimage['date'];
$i_like=$fimage['likes'];
$i_unlike=$fimage['down'];
4

1 回答 1

3

您在句子中将 theJOIN与缺少的WHERE子句混合在一起(这就是您使用 的原因DISTINCT,因为根本没有连接条件并且您有重复的行)

$followers = mysql_query("SELECT distinct follow.*    
 FROM follow JOIN photos ON (follow.followers  = $tu_id)
order by photos.date DESC");

这相当于:

$followers = mysql_query("SELECT distinct follow.*
FROM follow, photos WHERE (follow.followers  = $tu_id)
order by photos.date DESC");

该连接为您提供了整个笛卡尔积 ( FROM follow JOIN photos) where 列follow.followers = $tu_id,其中包括“关注”和“照片”表之间未真正连接的行。要丢弃行,您需要whereAFTER连接条件后的子句。

无论如何,你正在混合一些东西。我已经多次分析了您的代码,但当您选择列并仅使用其中一个列中的 where 条件时,我不明白您为什么要将“照片”与“关注”表连接起来。

我将举一个例子来说明你正在尝试做的事情。只是 SQL 有意义的东西。我会让PHP给你。这些是表格 USERS、PHOTO 和 FOLLOW

USERS:
ID      USERNAME
--      --------
01      John
02      Smith
03      Rambo

PHOTO: (just the relevant columns)
ID      IMG     DATE            USER_ID  
--      -----   -----------     ------- 
01      a.jpg   01-02-2013      03  (Rambo)
02      b.jpg   30-07-2013      03  (Rambo)
03      c.jpg   04-04-2012      02  (Smith)
04      d.jog   04-04-2013      01  (John)

FOLLOW:
ID      FOLLOWERS   FOLLOWING
--      --          --
01      01          02      (John is following Smith)
02      01          03      (John is following Rambo)
03      02          01      (Smith is following John)
04      03          01      (Rambo is following John)
05      02          03      (Smith is following Rambo)

通过此设置,我们希望显示来自给定用户名的用户的图像,并按图像日期排序。因此,给定的用户名是“John”。那就是ID“01”。如果我们只需要约翰关注的人的图像,那么:

SELECT PHOTO.IMG 
FROM FOLLOW JOIN PHOTO ON (PHOTO.USER_ID = FOLLOW.FOLLOWING) 
WHERE FOLLOW.FOLLOWERS = 01
ORDER BY PHOTO.DATE DESC;

我们正在加入“Photo.User_ID”和“Follow.following”,按日期顺序排列。这将制作一个临时表,如:

PHOTO.ID    PHOTO.IMG   PHOTO.DATE  PHOTO.USER_ID   FOLLOW.ID   FOLLOW.FOLLOWERS    FOLLOW.FOLLOWING
--          -----       ----------  -------         --          --                  --
02          b.jpg       30-07-2013  03  (Rambo)     02          01                  03      (John is following Rambo)
04          d.jog       04-04-2013  01  (John)      04          03                  01      (Rambo is following John)
04          d.jog       04-04-2013  01  (John)      03          02                  01      (Smith is following John)
01          a.jpg       01-02-2013  03  (Rambo)     02          01                  03      (John is following Rambo)
03          c.jpg       04-04-2012  02  (Smith)     01          01                  02      (John is following Smith)

然后该WHERE子句将仅选择跟随者是我们想要的 user_id (01) 的那些行。那是:

PHOTO.ID    PHOTO.IMG   PHOTO.DATE  PHOTO.USER_ID   FOLLOW.ID   FOLLOW.FOLLOWERS    FOLLOW.FOLLOWING
--          -----       ----------  -------         --          --                  --
02          b.jpg       30-07-2013  03  (Rambo)     02          01                  03      (John is following Rambo)
01          a.jpg       01-02-2013  03  (Rambo)     02          01                  03      (John is following Rambo)
03          c.jpg       04-04-2012  02  (Smith)     01          01                  02      (John is following Smith)

因此,作为 John,我们将看到 Rambo 的 2 张照片和 Smith 的 1 张照片。按日期排序。

希望这对您有所帮助。

于 2013-08-08T17:44:06.437 回答