简体   繁体   English

从另一个表中选择日期

[英]Select date from another table

I really need your help. 我真的需要你的帮助。 Please help me out to get this to work. 请帮我解决这个问题。

I have these two tables, the relation between them is the user_id . 我有这两个表, relation之间的relationuser_id I accomplished to select who I follow and to display their images into my wall. 我完成了选择我关注的人并将他们的图像显示在我的墙上。 However, I have this strange issue: the photos are sorted by DESC as i want, but they are sorted by the user_id . 但是,我有这个奇怪的问题:照片按照我的要求按DESC排序,但它们按user_id排序。 This means that whoever I follow first, their image will be sorted first by DESC . 这意味着无论我先跟随谁,他们的图像将首先由DESC排序。

I tried almost every possible way to get the photos to be sorted base on DESC newest photo as the top, but I couldn't. 我尝试了几乎所有可能的方法来根据DESC最新照片将照片分类为顶部,但我不能。 Here are my tables and i'll show you every possible thing I tried: 这是我的桌子,我会告诉你我尝试的每一件事:

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`)
}

One

followers_photo.php, where I retrieve the following id from: 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 which i display the images here. index.php我在这里显示图像。

while($uim = mysql_fetch_assoc($followers)){ $i_id = $uim['following']; while($ uim = mysql_fetch_assoc($ followers)){$ i_id = $ uim ['follow']; $followers_image = mysql_query("SELECT distinct photos.* FROM photos JOIN follow ON (photos.user_id = $i_id) GROUP BY RAND() order by date DESC"); $ followers_image = mysql_query(“选择不同的照片。* FROM照片JOIN跟随ON(photos.user_id = $ i_id)GROUP BY RAND()按日期排序DESC”);


The above is working, but as I mentioned it sorts the images based on date DESC and the user_id which I don't want. 以上是有效的,但正如我所提到的,它根据日期DESC和我不想要的user_id对图像进行排序。 I want it to stop sorting the image based on the user_id 我希望它停止基于user_id对图像进行排序

Two

followers_photo.php 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'];
$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);

index.php 的index.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 ");

The above does the same as in the first step. 以上与第一步相同。 Could anyone point me to the right way? 谁能指出我正确的方法? Sort the photos from the people I follow based on date DESC , who posts last comes first. 根据date DESC对我关注的人进行排序,最后发布的是date DESC Thanks guys, I appreciate your help a lot. 谢谢你们,我非常感谢你们的帮助。 And sorry for my bad English. 抱歉我的英语不好。

Update 更新

now with the solution that vegatripy give , images are duplicating and not order as wanted. 现在有了vegatripy给出的解决方案,图像是重复的,而不是按照需要订购。 on left is what is showing now. 左边是现在显示的内容。 what i want is to sort as the right image. 我想要的是排序为正确的形象。 any ideas? 有任何想法吗? http://i.stack.imgur.com/Cod6z.jpg http://i.stack.imgur.com/Cod6z.jpg

Solved 解决了

    $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'];

You're mixing the JOIN with the missing WHERE clause in your sentences (and that's why you used the DISTINCT , because there's no join condition at all and you had repeated rows) 你在句子中混合了JOIN和缺少的WHERE子句(这就是你使用DISTINCT的原因,因为根本没有连接条件 ,你有重复的行)

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

Which is equivalent to: 这相当于:

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

That join is giving you the whole cartesian product ( FROM follow JOIN photos ) where column follow.followers = $tu_id , that includes not truly joined rows between "follow" and "photos" tables . 该连接为您提供了整个笛卡尔积( FROM follow JOIN photos ),其中列follow.followers = $tu_id ,其中包括“follow”和“photos”表之间没有真正连接的行。 To discard rows, you'll need the where clause AFTER the join codition . 要丢弃行,您需要在联接编码之后使用where子句。

Anyway, you're mixing things a little bit. 无论如何,你正在混合一些东西。 I've analyzed your code several times and I don't get why you are joining "photos" with "follow" tables, when you're selecting columns and using the where condition from just one of them. 我已经多次分析过您的代码,当您选择列并使用其中一个的where条件时,我不明白为什么要将“照片”与“跟随”表联系起来。

I'm going to set an example of what you're trying to do. 我将举一个你想要做的事情的例子。 Just the SQL meaningful stuff. 只是SQL有意义的东西。 I'll let the PHP to you. 我会把PHP给你。 These are the tables USERS, PHOTO and FOLLOW 这些是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)

With this set up, we want to display the images from the users of a given username is following, order by date of image. 通过此设置,我们希望显示来自给定用户名的用户的图像,按照图像的日期排序。 So, the given username is "John". 所以,给定的用户名是“John”。 That is ID "01". 那是ID“01”。 If we just need the images from the people that John is following, then: 如果我们只需要约翰所关注的人的图像,那么:

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

We are joining "Photo.User_ID" with "Follow.following", order by date desc. 我们正在加入“Photo.User_ID”和“Follow.following”,按日期排序desc。 This will make a temp table like: 这将使临时表如下:

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)

Then the WHERE clause will select only those rows where the follower is the user_id we want (01). 然后WHERE子句将只选择跟随者是我们想要的user_id的那些行(01)。 That's: 那是:

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)

So as John we will see 2 photos from Rambo and 1 from Smith. 所以约翰我们将看到兰博的2张照片和史密斯的1张照片。 Ordered by date. 按日期排序。

Hope this help you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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