1

我想使用 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,因此我不知道用于完成此操作的语法。任何帮助将不胜感激。

4

1 回答 1

0

如果我正确地假设:

  • atn_...名为、cty_...seg_...和的字段分别s2a_...属于attractioncitysegmentseg2atn
  • 命名的字段..._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
        )
;
于 2012-10-22T15:45:08.393 回答