2014-10-09 94 views
1

Fiddle Example 1合併兩個結果集爲一個在MySQL

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; 

我有一個民意調查,允許用戶投票支持y建議的產品與他們瀏覽的產品相似。他們被允許一次投多個項目並留下評論。每個投票項目使用插入到表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 
+2

也許您在尋找'UNION'? - http://dev.mysql.com/doc/refman/5.0/en/union.html – LcSalazar 2014-10-09 15:03:21

回答

2

通過使用兩個querys結果和工會都試試下面的辦法:

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 
+0

標記爲正確的答案,如果它對你有用.. :) – Karunakar 2014-10-09 15:29:55

相關問題