我有两张表MESSAGES
和USERS
:
MESSAGES
:
|id|user_id|message|1st number|2nd number|
USERS
:
|user_id|name|
我试图显示消息,并通过user_id获取消息旁边的用户名。是否可以只在一个查询中执行此操作?
此时我的演示查询看起来像这样,但它不起作用。
SELECT *
FROM messages
WHERE 1st number BETWEEN $x AND $z
AND 2nd BETWEEN $x AND $z
LEFT JOIN users
ON users.user_id= messages.user_id
ORDER BY time DESC
我得到的错误:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便使用正确的语法在LEE JOIN用户附近ON WHERE users.user_id = messages.user_id ORDER BY time DESC'在第5行
答案 0 :(得分:0)
您的SQL存在一些问题:
类似
SELECT *
FROM messages
LEFT JOIN users ON users.user_id=messages.user_id
WHERE `1st number` BETWEEN $x AND $z
AND `2nd number` BETWEEN $x AND $z
ORDER BY time DESC
应该有效
答案 1 :(得分:0)
请试一试。
SELECT
*
FROM
messages
LEFT JOIN users ON users.user_id = messages.user_id
WHERE
`1st number` BETWEEN $x AND $z
AND
`2nd number` BETWEEN $x AND $z
ORDER BY time DESC