当我尝试此查询时:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author = ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
它正常工作。
但我需要这个:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author <> ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
这次查询必须选择第一个查询的相反,但它只是选择所有作者。我尝试了!=
和NOT IN
,但结果是一样的。
为什么?为什么<>
无法按预期工作?
答案 0 :(得分:2)
我认为会改变
and author = any...
to
and NOT author = any...
会起作用......但如果没有,那么我会尝试做一个左连接并寻找null。由于作者是&#34; mid&#34;从sds_actions中,我会把它写成......
SELECT
sp.*
FROM
sds_posts sp
LEFT JOIN sds_actions sa
on sp.author = sa.mid
AND sa.whoami = '710'
AND sa.type = 'block'
WHERE
sp.topic_id = '2439'
AND sp.status = '1'
AND sp.deleted = '0'
AND sa.mid IS NULL
ORDER by
sp.id ASC
LIMIT 50
答案 1 :(得分:0)
您可以尝试将author = any(...)
更改为author IN (...)
并将author <> any(...)
更改为author NOT IN (...)