[英]How can I select and order rows from one table based on values contained in another?
I am currently working with two tables. 我目前正在使用两个表。
status: 状态:
id INT(4) AUTO_INCREMENT
username VARCHAR(20)
text LONGTEXT
datetime VARCHAR(25)
attachment VARCHAR(11)
timestamp VARCHAR(50)
friends: 朋友:
id INT(2) AUTO_INCREMENT
added INT(1)
userto VARCHAR(32)
userfrom VARCHAR(32)
I would like to add the option for a user to filter statuses for only their friend's statuses, and display them with the newest one first. 我想为用户添加选项,以仅针对其朋友的状态过滤状态,并以最新的状态显示它们。 By this I mean most recent status, not newest per friend, which I suspect can be done with:
我的意思是,我的意思是可以通过以下方式获得最近的状态,而不是每个朋友的最新状态:
ORDER BY status.id DESC
How would I order the statuses based on the statuses of the users on the person's friends list? 我如何根据此人的朋友列表上用户的状态排序状态?
Try this 尝试这个
SELECT status.*
FROM status
JOIN friends
ON status.username = friends.userto
WHERE friends.userfrom = '$username'
UNION
SELECT status.*
FROM status
JOIN friends
ON status.username = friends.userfrom
WHERE friends.userto = '$username'
ORDER BY status.id DESC;
this query loads statuses from usernames on either end of the friendship relationship that includes $username. 该查询从包含$ username的友谊关系的任一端从用户名加载状态。 Union is used to glue two lists top to bottom.
联合用于从上到下粘合两个列表。 Join is used to glue two lists side by side and align them on the author's username.
Join用于并排粘贴两个列表,并使它们与作者的用户名对齐。
Well, without any sample data it would be hard to do this for sure, but I would walk through it this way. 好吧,如果没有任何样本数据,那么肯定很难做到这一点,但是我将以这种方式进行介绍。
The statuses we want to show (regardless of order) are those the user is friends with. 我们想要显示的状态(与顺序无关)是用户与之成为朋友的状态。 So, let's get the friends of
Adam
for example: 因此,让我们以
Adam
的朋友为例:
SELECT DISTINCT userto
FROM friends
WHERE userfrom = 'Adam'
UNION
SELECT DISTINCT userfrom
FROM friends
WHERE userto = 'Adam';
At this moment, I should point out that in your friends table the usernames are VARCHAR(32) and in the status table they are VARCHAR(20), I would assume they should be the same. 此时,我应该指出,在您的friends表中,用户名是VARCHAR(32),在状态表中,用户名是VARCHAR(20),我认为它们应该相同。
So, now you can filter the status based on whether or not the username is in the above subquery, and you could order by id descending, assuming you add them in order, but the best way would be to order by the timestamp on the status: 因此,现在您可以根据用户名是否在上述子查询中来过滤状态,并且可以假设ID按顺序添加, 可以按ID降序进行排序,但最好的方法是按状态的时间戳进行排序:
SELECT *
FROM status
WHERE username IN
(SELECT DISTINCT userto
FROM friends
WHERE userfrom = 'Adam'
UNION
SELECT DISTINCT userfrom
FROM friends
WHERE userto = 'Adam')
ORDER BY datetime DESC;
EDIT 编辑
I would also rethink your variables for datetime and timestamp, as well as rethink the name. 我还将重新考虑您的变量的日期时间和时间戳,以及重新考虑名称。 While these are valid, they are also reserved words (DATETIME and TIMESTAMP are data types in MySQL).
尽管这些有效,但它们也是保留字(DATETIME和TIMESTAMP是MySQL中的数据类型)。 A possible reconstruction of your status table could be:
状态表的可能重建可能是:
id INT(4) AUTO_INCREMENT
username VARCHAR(32)
statusText LONGTEXT
statusPostDate DATETIME
attachment VARCHAR(11)
The DATETIME variable will hold both the date and time portions for you. DATETIME变量将为您同时保留日期和时间部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.