【问题标题】:MySql order based on the value from other tableMySql 订单基于其他表中的值
【发布时间】:2017-10-17 05:45:04
【问题描述】:

我有两张桌子 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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    将您的加入放在 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
    

    【讨论】:

    • 很高兴它帮助... :)
    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多