MySql命令基于其他表的值

时间:2017-10-17 05:45:05

标签: php mysql

我有两张桌子started_chatschats,我想根据上次发送的聊天订阅started_chats。这是我的查询

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的所有内容。然后我加入聊天表以选择从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

1 个答案:

答案 0 :(得分:0)

在From

之后放置您的联接
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