在mysql中将两个结果集合并为一个

时间:2014-10-09 14:53:37

标签: html mysql sql join group-by

Fiddle Example 1

Fiddle Example 2

有人可以告诉我是否可以将以下两个结果集合并为一个以便不使用两个单独的查询?

PRODUCT_PAGE_ID PRODUCT_PAGE_NAME   SIMILAR_PRODUCT SIMILAR_PRODUCT_ID  RESULT
22              Nokia 8234          E821            777                 2
22              Nokia 8234          HTC 2811        444                 2


PRODUCT_PAGE_ID PRODUCT_PAGE_NAME   VOTER_NAME  COMMENT
22              Nokia 8234          John        blahblahblah
22              Nokia 8234          David       xxxxxxxxxxxx
22              Nokia 8234          Peter       yyyyyyyyyyyy
22              Nokia 8234          John        zzzzzzzzzzzz

我的预期结果应该是:

PRODUCT_PAGE_ID   PRODUCT_PAGE_NAME  SIMILAR_PRODUCT  SIMILAR_PRODUCT_ID    RESULT VOTER_NAME COMMENT
    22              Nokia 8234       E821             777                   2      NULL       NULL
    22              Nokia 8234       HTC 2811         444                   2      NULL       NULL
    22              Nokia 8234       NULL             NULL                  NULL   John       blahblahblah
    22              Nokia 8234       NULL             NULL                  NULL   David      xxxxxxxxxxxx
    22              Nokia 8234       NULL             NULL                  NULL   Peter      yyyyyyyyyyyy
    22              Nokia 8234       NULL             NULL                  NULL   John       zzzzzzzzzzzz

第一个结果集使用此查询

SELECT p.product_page_id,p.name AS product_page,
       p2.name AS similar_product,
       p2.product_page_id AS similar_product_id,COUNT(poll.choice) AS result
FROM poll 
INNER JOIN product p ON poll.product_page_id = p.product_page_id
INNER JOIN product p2 ON poll.choice = p2.product_page_id
WHERE poll.product_page_id = 22
GROUP BY poll.choice
ORDER BY result DESC

第二个使用

SELECT pc.product_page_id,p.name AS product_page_name,
       u.name AS voter_name,pc.comment
FROM `poll_comment` pc INNER JOIN `user` u 
ON u.user_id = pc.user_id
INNER JOIN `product` p ON pc.product_page_id = p.product_page_id 
WHERE pc.product_page_id = 22
LIMIT 10;

我进行了一项民意调查,允许用户投票选出与他们正在浏览的产品类似的任何建议产品。他们可以一次投票多个项目并发表评论。每个投票项目使用插入表poll的单行。我试图在一个查询中提取投票数以及选民的10条评论。问题是我提出的查询无法获得正确的投票计数或产品评论。

表架构:

CREATE TABLE poll
    (`user_id` int,`product_page_id`int,`choice` int)
;

INSERT INTO poll
    (`user_id`,`product_page_id`,`choice`)
VALUES
    (1,22,444),
    (1,22,777),
    (2,22,444),
    (3,22,777)

;
CREATE TABLE poll_comment
    (`user_id` int,`product_page_id`int,`comment` varchar(40))
;

INSERT INTO poll_comment
    (`user_id`,`product_page_id`,`comment`)
VALUES
    (1,22,'blahblahblah'),
    (2,22,'xxxxxxxxxxxx'),
    (3,22,'yyyyyyyyyyyy'),
    (1,33,'zzzzzzzzzzzz'),
    (2,33,'kkkkkkkkkkkk')
;


CREATE TABLE user
    (`user_id` int, `name` varchar(30))
;

INSERT INTO user
    (`user_id`, `name`)
VALUES
    (1,'John'),
    (2,'David'),
    (3,'Peter'),
    (4,'May')
;

CREATE TABLE product
    (`product_page_id` int, `name` varchar(30))
;

INSERT INTO product
    (`product_page_id`, `name`)
VALUES
    (1,'Sony A821'),
    (22,'Nokia 8234'),
    (444,'HTC 2811'),
    (777,'E821')

;

这是我的尝试(Fiddle):

SELECT * FROM (
  SELECT p.name AS product_page,poll.product_page_id,p2.name AS similar_product, 
         COUNT(poll.choice) As vote_result 
  FROM `poll` 
    INNER JOIN product p ON poll.product_page_id = p.product_page_id
    INNER JOIN `product` p2 ON poll.choice = p2.product_page_id  
  GROUP BY poll.choice
  ORDER By vote_result desc
)TAB1
JOIN
(
  SELECT pc.comment,pc.product_page_id,u.name 
  FROM `poll_comment` pc
   INNER JOIN `product` p ON pc.product_page_id = p.product_page_id
   INNER JOIN `user` u ON u.user_id = pc.user_id
  LIMIT 10
)TAB2
ON TAB1.product_page_id = TAB2.product_page_id
WHERE TAB1.product_page_id = 22

1 个答案:

答案 0 :(得分:2)

通过使用两个查询的结果并联合所有来尝试以下方法:

select PRODUCT_PAGE_ID,PRODUCT_PAGE_NAME,SIMILAR_PRODUCT,SIMILAR_PRODUCT_ID,RESULT,null as VOTER_NAME , null as COMMENT from query1
UNION ALL
select PRODUCT_PAGE_ID,PRODUCT_PAGE_NAME,null as SIMILAR_PRODUCT,null as SIMILAR_PRODUCT_ID,null as RESULT,VOTER_NAME,COMMENT from query2

基于您的数据的确切答案:

select PRODUCT_PAGE_ID,product_page as PRODUCT_PAGE_NAME,SIMILAR_PRODUCT,SIMILAR_PRODUCT_ID,RESULT,null as VOTER_NAME , null as COMMENT from (SELECT p.product_page_id,p.name AS product_page,
       p2.name AS similar_product,
       p2.product_page_id AS similar_product_id,COUNT(poll.choice) AS result
FROM poll 
INNER JOIN product p ON poll.product_page_id = p.product_page_id
INNER JOIN product p2 ON poll.choice = p2.product_page_id
WHERE poll.product_page_id = 22
GROUP BY poll.choice
ORDER BY result DESC)temp

UNION ALL

select PRODUCT_PAGE_ID,PRODUCT_PAGE_NAME,null as SIMILAR_PRODUCT,null as SIMILAR_PRODUCT_ID,null as RESULT,VOTER_NAME,COMMENT from (SELECT pc.product_page_id,p.name AS product_page_name,
       u.name AS voter_name,pc.comment
FROM `poll_comment` pc INNER JOIN `user` u 
ON u.user_id = pc.user_id
INNER JOIN `product` p ON pc.product_page_id = p.product_page_id 
WHERE pc.product_page_id = 22)TEMP2