使用UNION或任何其他解决方案将两个查询合并为一个

时间:2014-08-06 08:28:19

标签: mysql sql left-join union

查询1访问用户喜欢/不喜欢或其他活动的历史记录:FIDDLE

查询所有产品的记录:FIDDLE

第一个查询将为该特定用户生成此结果:

NAME    MODEL   ISBOOKED    USER_MIN    USER_MAX    USER_LIKE_TYPE
Tom     TOYA    1           100         300         0 
Tom     TOYB    1           (null)      (null)      1
Tom     ToyC    (null)      (null)      (null)      (null)

第二个查询将提供所有产品的记录

MODEL   RESALE_USERSNO  AVG_MIN AVG_MAX LIKES   DISLIKES
ToyA    1               100     300     1       0 
ToyB    0               (null)  (null)  1       1
ToyC    1               50      400     (null)  (null)

是否可以将用户记录和一般产品的记录合并为如下所示:

MODEL RESALE_USERSNO AVG_MIN AVG_MAX LIKES DISLIKES NAME ISBOOKED USER_MIN USER_MAX USER_LIKE_TYPE
ToyA    1            100     300     1      0       Tom   1        100       300       0 
ToyB    0            (null) (null)   1      1       Tom   1        (null)    (null)    1
ToyC    1            50      400     (null) (null)  Tom   (null)    (null)    (null)   (null)

这是我使用UNION的尝试(FIDDLE),但它似乎无法正常工作

查询1

SELECT a.NAME,b.MODEL,c.ISBOOKED,d.MIN_ AS USER_MIN,
       d.MAX_ AS USER_MAX,e.LIKE_TYPE AS USER_LIKE_TYPE
     FROM `USER_ID` a
     JOIN `MODEL_ID` b
       ON b.MODEL_NUMBER IN ('AAA','BBB','CCC')
LEFT JOIN `BOOKMARK` c 
       ON c.USER_ID = a.USER_ID
      AND c.MODEL_ID = b.MODEL_ID         
LEFT JOIN `RESALE_POLL` d 
       ON d.USER_ID = a.USER_ID
      AND d.MODEL_ID = b.MODEL_ID 
LEFT JOIN `LIKE_DISLIKE` e
       ON e.USER_ID = a.USER_ID
      AND e.MODEL_ID = b.MODEL_ID
    WHERE a.FACEBOOK_ID = 'FB1111'

查询2

SELECT b.MODEL,COUNT( c.USER_ID ) AS RESALE_USERSNO, 
        ROUND( AVG( c.MIN_ ) , 1 ) AS AVG_MIN, 
        ROUND( AVG( c.MAX_ ) , 1 ) AS AVG_MAX, 
        b.MODEL_ID, a.LIKES, a.DISLIKES
        FROM  `LIKES_TOTAL` a
        RIGHT JOIN  `MODEL_ID` b 
          ON a.MODEL_ID = b.MODEL_ID
        LEFT JOIN  `RESALE_POLL` c 
          ON c.MODEL_ID = b.MODEL_ID
        WHERE b.MODEL_NUMBER IN ('AAA','BBB','CCC')
        GROUP BY b.MODEL_NUMBER

尝试失败:

SELECT b.MODEL,
       NULL AS MODEL_ID,
       a.NAME,
       NULL AS RESALE_USERSNO,
       NULL AS AVG_MIN,
       NULL AS AVG_MAX,
       NULL AS LIKES,
       NULL AS DISLIKES,
       d.MIN_ AS USER_MIN,
       d.MAX_ AS USER_MAX,
       e.LIKE_TYPE AS USER_LIKE_TYPE,
       c.ISBOOKED
     FROM `USER_ID` a
     JOIN `MODEL_ID` b
       ON b.MODEL_NUMBER IN ('AAA','BBB','CCC')
LEFT JOIN `BOOKMARK` c 
       ON c.USER_ID = a.USER_ID
      AND c.MODEL_ID = b.MODEL_ID         
LEFT JOIN `RESALE_POLL` d 
       ON d.USER_ID = a.USER_ID
      AND d.MODEL_ID = b.MODEL_ID 
LEFT JOIN `LIKE_DISLIKE` e
       ON e.USER_ID = a.USER_ID
      AND e.MODEL_ID = b.MODEL_ID
    WHERE a.FACEBOOK_ID = 'FB1111'

UNION

SELECT  b.MODEL,
        b.MODEL_ID,
        NULL AS NAME,
        COUNT( c.USER_ID ) AS RESALE_USERSNO, 
        ROUND( AVG( c.MIN_ ) , 1 ) AS AVG_MIN, 
        ROUND( AVG( c.MAX_ ) , 1 ) AS AVG_MAX,
        a.LIKES, 
        a.DISLIKES,
        NULL AS ISBOOKED,
        NULL AS USER_MIN,
        NULL AS USER_MAX,
        NULL AS USER_LIKE_TYPE
        FROM  `LIKES_TOTAL` a
        RIGHT JOIN  `MODEL_ID` b 
          ON a.MODEL_ID = b.MODEL_ID
        LEFT JOIN  `RESALE_POLL` c 
          ON c.MODEL_ID = b.MODEL_ID
        WHERE b.MODEL_NUMBER IN ('AAA','BBB','CCC')
        GROUP BY b.MODEL_NUMBER

1 个答案:

答案 0 :(得分:1)

使用Join代替union。将2个结果视为单独的内部选项卡,并将它们连接到模型代码上以获得所需的结果。

在这里小提琴:http://sqlfiddle.com/#!2/6bfcb4/11

SELECT * FROM 
(
SELECT a.NAME,b.MODEL,c.ISBOOKED,d.MIN_ AS USER_MIN,
       d.MAX_ AS USER_MAX,e.LIKE_TYPE AS USER_LIKE_TYPE
     FROM `USER_ID` a
     JOIN `MODEL_ID` b
       ON b.MODEL_NUMBER IN ('AAA','BBB','CCC')
LEFT JOIN `BOOKMARK` c 
       ON c.USER_ID = a.USER_ID
      AND c.MODEL_ID = b.MODEL_ID         
LEFT JOIN `RESALE_POLL` d 
       ON d.USER_ID = a.USER_ID
      AND d.MODEL_ID = b.MODEL_ID 
LEFT JOIN `LIKE_DISLIKE` e
       ON e.USER_ID = a.USER_ID
      AND e.MODEL_ID = b.MODEL_ID
    WHERE a.FACEBOOK_ID = 'FB1111'
)TAB1

JOIN

(SELECT b.MODEL,COUNT( c.USER_ID ) AS RESALE_USERSNO, 
        ROUND( AVG( c.MIN_ ) , 1 ) AS AVG_MIN, 
        ROUND( AVG( c.MAX_ ) , 1 ) AS AVG_MAX, 
        b.MODEL_ID, a.LIKES, a.DISLIKES
        FROM  `LIKES_TOTAL` a
        RIGHT JOIN  `MODEL_ID` b 
          ON a.MODEL_ID = b.MODEL_ID
        LEFT JOIN  `RESALE_POLL` c 
          ON c.MODEL_ID = b.MODEL_ID
        WHERE b.MODEL_NUMBER IN ('AAA','BBB','CCC')
        GROUP BY b.MODEL_NUMBER
)TAB2

ON TAB1.MODEL = TAB2.MODEL