我只是好奇我是否正确地执行此代码 - 它似乎可以在我的测试服务器上工作,但是我知道要让它一直存在直到我知道我正在使用OR语句正确。
SELECT privatemessages.threadID, privatemessages.messageID, privatemessages.message_body, privatemessages.uid
FROM privatemessages, privatethreads
WHERE privatemessages.threadID =1
AND privatethreads.threadID = privatemessages.threadID
AND privatethreads.to_user
OR privatethreads.from_user =1
LIMIT 0 , 30
答案 0 :(得分:0)
没有。你没有。并且不要使用隐式连接语法...
SELECT m.threadID
, m.messageID
, m.message_body
, m.uid
FROM privatemessages m
JOIN privatethreads t
ON t.threadID = m.threadID
WHERE m.threadID = 1
AND 1 IN (t.to_user,t.from_user)
LIMIT 0 , 30
答案 1 :(得分:0)
第一步应该是添加括号:
SELECT privatemessages.threadID, privatemessages.messageID, privatemessages.message_body, privatemessages.uid
FROM privatemessages, privatethreads
WHERE privatemessages.threadID =1
AND privatethreads.threadID = privatemessages.threadID
AND (privatethreads.to_user
OR privatethreads.from_user =1)
LIMIT 0 , 30
如果你混合使用and
和or
,你可能总是想要使用括号。
逗号连接语法也是某种"脏",也许您想以下列方式编写查询,以便更清楚地说明哪个部分是连接以及哪个部分是约束:
SELECT privatemessages.threadID, privatemessages.messageID, privatemessages.message_body, privatemessages.uid
FROM privatemessages
INNER JOIN privatethreads
ON privatethreads.threadID = privatemessages.threadID
WHERE privatemessages.threadID =1
AND (privatethreads.to_user
OR privatethreads.from_user =1)
LIMIT 0 , 30
答案 2 :(得分:0)
不,您可以执行以下任一操作:
SELECT
privatemessages.threadID,
privatemessages.messageID,
privatemessages.message_body,
privatemessages.uid
FROM privatemessages, privatethreads
WHERE privatemessages.threadID =1
AND privatethreads.threadID = privatemessages.threadID
AND (privatethreads.to_user=1 OR privatethreads.from_user =1)
LIMIT 0 , 30
OR
SELECT
privatemessages.threadID,
privatemessages.messageID,
privatemessages.message_body,
privatemessages.uid
FROM privatemessages, privatethreads
WHERE privatemessages.threadID =1
AND privatethreads.threadID = privatemessages.threadID
AND 1 IN (privatethreads.to_user, privatethreads.from_user)
LIMIT 0 , 30