繁体   English   中英

MySQL订单基于其他表的值

[英]MySql order based on the value from other table

我有两个表started_chatschats ,我想订购started_chats基于上次聊天sent.So这里是我的查询

SELECT started_chats.* 
FROM started_chats 
WHERE User1='1' OR User2='1' 
INNER JOIN chats ON (chats.From_='1' OR chats.To_='1') 
AND (chats.To= started_chats.User1 OR
     chats.To= started_chats.User2)
ORDER BY chats.Date DESC

所以首先我从started_chats中选择User1或User2等于1的所有内容。之后,我加入chats表以选择从1发送给其他用户的最后一个聊天(例如,如果User1为1,那么我正在从User1到User2)。但是此查询给我以下错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN chats ON (chats.From_='1' OR chats.To_='1') AND (chats.To= started_ch

加入后发件人

SELECT started_chats.* 
FROM started_chats 
INNER JOIN chats ON (chats.From_='1' OR chats.To_='1')
WHERE User1='1' OR User2='1' 
AND (chats.To= started_chats.User1 OR chats.To= started_chats.User2)
ORDER BY chats.Date DESC

暂无
暂无

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

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