我想使用MySQL创建以下两个查询的INTERSECTion:
1) SELECT atn_nid, atn_select_flag, atn_image_name
FROM city, attraction WHERE cty_nid = atn_cty_nid AND cty_nid = 5;
2) SELECT atn_nid, s2a_image_select_flag, atn_image_name
FROM segment, seg2atn, attraction WHERE seg_nid = s2a_seg_nid AND
s2a_atn_nid = atn_nid and seg_nid = 68 AND
s2a_image_select_flag = true;
注意:查询1中的atn_select_flag和s2a_image_select_flag都是布尔值
由于MYSQL由于某种原因不支持INTERSECT,我对用于实现此目的的语法感到茫然。任何帮助将不胜感激。
答案 0 :(得分:0)
如果我正确地假设:
atn_...
,cty_...
,seg_...
和s2a_...
的字段属于attraction
,city
,segment
,以及分别为seg2atn
。..._nid
的字段是主键。atn_cty_nid
,s2a_seg_nid
等是外键。然后您的第一个查询等同于:
SELECT atn_nid, atn_select_flag, atn_image_name
FROM attraction
WHERE aty_cty_nid = 5
;
并且您的第二个查询等同于:
SELECT atn_nid, TRUE, atn_image_name
FROM attraction
WHERE atn_id IN
( SELECT s2a_atn_nid
FROM seg2atn
WHERE s2a_seg_nid = 68
AND s2a_image_select_flag = TRUE
)
;
所以他们的交集就是这样:
SELECT atn_nid, TRUE AS atn_select_flag, atn_image_name
FROM attraction
WHERE atn_cty_nid = 5
AND atn_nid IN
( SELECT s2a_atn_nid
FROM seg2atn
WHERE s2a_seg_nid = 68
AND s2a_image_select_flag = TRUE
)
;
和(编辑添加)他们的联盟就是这样:
SELECT atn_nid, atn_select_flag, atn_image_name
FROM attraction
WHERE aty_cty_nid = 5
UNION
SELECT atn_nid, TRUE, atn_image_name
FROM attraction
WHERE atn_id IN
( SELECT s2a_atn_nid
FROM seg2atn
WHERE s2a_seg_nid = 68
AND s2a_image_select_flag = TRUE
)
;