ORDER BY column FROM another table

ORDER BY column FROM another table

我想通过 last_act 和他们一起订购用户的朋友。但是有关用户的信息在 table userslast_act 中 table 朋友.

我试过了,但没用:

SELECT m.* 
FROM users AS m 
WHERE m.username = '$username' 
JOIN friends AS p 
ORDER BY p.last_act DESC

您应该有一个用户朋友 table 的外键。 例如,说它是 userid。 那么上面的查询将是:

SELECT m.* 
FROM users AS m 
    JOIN friends AS p ON p.userid = m.id  
WHERE m.username = '$username' 
ORDER BY p.last_act DESC
SELECT m.*, f.* 
FROM users AS m  
JOIN friends AS p 
ON m.id = p.user_id
LEFT JOIN users AS f 
ON p.friend_id = f.id
WHERE m.username = '$username' 
ORDER BY p.last_act DESC

你的问题太笼统了,我们不知道表的结构,也没看到报错信息...

但是发现有顺序问题,它必须按 SELECT、FROM、JOIN、WHERE 进行。并且您需要使用 ON 来映射 JOIN 中的关系。

示例

SELECT * FROM table1 JOIN table2 ON table1.id = table2.foreign_id