我正在处理查询并且非常困难。我将它提炼到下面的内容。
我从feed中获取数据,其中user_id不是当前用户,这种情况是1,隐私设置小于2.我怎么想检查当前用户是否曾回复过,换句话说,prfor表中没有该行的当前用户的行。
此时我的逻辑已经消失了,我无法理解它应该如何运作。它是否需要一个子查询来处理这类事情,还是可以加入呢?
在最后的查询中,还有两个JOIN,但这似乎不会对下面的内容产生影响。
SELECT
pf.id,
pf.privacy
FROM
feed pf
WHERE
(
pf.user_id != 1
AND pf.privacy < 2
)
AND NOT (
SELECT
pd.p_for
FROM
prfor pd
WHERE
pd.user_id = 1
AND pd.pfeed_id = pf.id
AND pd.p_for = 1 LIMIT 1
)
感谢您提供的任何见解。
答案 0 :(得分:1)
您必须使用NOT EXISTS
:
SELECT pf.id, pf.privacy
FROM feed pf
WHERE (pf.user_id != 1 AND pf.privacy < 2) AND
NOT EXISTS (SELECT pd.p_for
FROM prfor pd
WHERE pd.pfeed_id = pf.id AND pd.user_id = 1 AND
pd.p_for = 1)
答案 1 :(得分:1)
如果pf.id不在prfor
,你应该使用或不用于测试 SELECT
pf.id, pf.privacy
FROM feed pf
WHERE (pf.user_id != 1 AND pf.privacy < 2) AND pf.user_id NOT IN
(SELECT pf.id FROM prfor pd WHERE pd.user_id = 1 AND pd.pfeed_id = pf.id AND pd.p_for = 1 LIMIT 1)
或不存在
You should use or not in
SELECT
pf.id, pf.privacy
FROM feed pf
WHERE (pf.user_id != 1 AND pf.privacy < 2) AND NOT EXIST
(SELECT pd.p_for FROM prfor pd WHERE pd.user_id = 1 AND pd.pfeed_id = pf.id AND pd.p_for = 1 LIMIT 1)
答案 2 :(得分:0)
SELECT
topt.*
FROM fields_options AS topt
WHERE (topt.field_id=2) AND topt.translation_of
NOT IN (SELECT top.translation_of FROM fields_options AS top WHERE top.field_id=2 AND top.translation_lang='ar' )
ORDER BY topt.translation_of