简体   繁体   English

如何根据一个表中的值选择另一个表中的行并对其排序?

[英]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.

相关问题 如何使存储过程根据另一个表中的行顺序更新一个表中的一列? - How can I make a stored procedure to update one column in one table based on the order of rows from another table? MySql - 如何根据在另一个表中加入的多个值从一个表中 select 行 - MySql - how to select rows from one table based on multiple values joined in another table 如何根据在另一个表中的日期之后加时间戳的行从一个 mysql 表中选择行 - How can I select rows from one mysql table based on the row being timestamped after a date in another table 如何从一个表中选择行作为CSV并根据键插入另一个表? - How do I select rows from one table as CSV and insert into another table based on a key? 如何将行从一个MySQL表复制到另一个表并基于变量更新值之一 - How do I copy rows from one MySQL table to another and update one of the values based on a variable 从一个表中选择行并根据另一表中的行调整值 - Select rows from one table and adjust the values based on rows in another table 如何使用联接基于另一个表中的行从一个表中获取mysql行? - How can I use joins to fetch mysql rows from one table based on rows that are found in another? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another 如何从一个表中选择行并按另一表中一列的总和对其进行排序? - How to select rows from one table and order it by the sum of a column from another table? 如何在MySQL中基于另一个表中的不匹配项从一个表中进行选择 - how can I select from one table based on non matches in another in mySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM