2016-12-27 57 views
-2

我得到的數據。合併多個MySQL查詢使用3個不同的MySQL查詢,並加入4個不同的表返回不同的列

MySQL查詢1

SELECT tb1.request_item_id AS most_requested_item_id, tb2.item_name AS most_requested_item_name 
FROM `requests` tb1 
LEFT JOIN `items` tb2 ON tb1.request_item_id = tb2.item_id 
GROUP BY tb1.request_item_id 
ORDER BY COUNT(*) DESC 
LIMIT 3 

查詢1輸出

most_requested_item_id | most_requested_item_name 
     87    | Poster FLS 
     95    | Sample Item 4 
     89    | Earth 

查詢2

SELECT tb1.user_credit AS highest_credit_value, tb1.user_full_name AS highest_credit_user 
    FROM users tb1 
    ORDER BY tb1.user_credit 
    DESC LIMIT 3 

查詢2輸出

highest_credit_value | highest_credit_user 
      140   | User A 
      11   | User B 
      10   | User C 

查詢3

SELECT tb1.credit_user_id AS credit_monthly_user, 
     SUM(tb1.credit_amount) AS totalCredit_monthly, 
     tb2.user_full_name AS monthly_credit_user 
FROM `credit_log` tb1 
LEFT JOIN users tb2 ON tb1.credit_user_id = tb2.user_id 
WHERE tb1.credit_date BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() 
GROUP BY credit_user_id 
ORDER BY totalCredit_monthly DESC 
LIMIT 3 

查詢3輸出

 credit_monthly_user | totalCredit_monthly Descending 1 | monthly_credit_user 
    User C    | 350        | User D 
    User E    | 170        | User F 
    User G    | 70        | User H 

我的SQL查詢

(SELECT tb1.request_item_id AS most_requested_item_id, tb2.item_name AS most_requested_item_name 
FROM `requests` tb1 LEFT JOIN `items` tb2 
ON tb1.request_item_id = tb2.item_id 
GROUP BY tb1.request_item_id 
ORDER BY COUNT(*) DESC 
LIMIT 3) 
UNION 
(SELECT tb1.user_credit AS highest_credit_value, tb1.user_full_name AS highest_credit_user 
FROM users tb1 
ORDER BY tb1.user_credit DESC 
LIMIT 3) 
UNION 
(SELECT tb1.credit_user_id AS credit_monthly_user, SUM(tb1.credit_amount) AS totalCredit_monthly, tb2.user_full_name AS monthly_credit_user 
FROM `credit_log` tb1 LEFT JOIN users tb2 
ON tb1.credit_user_id = tb2.user_id 
WHERE tb1.credit_date 
BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() 
GROUP BY credit_user_id 
ORDER BY totalCredit_monthly 
DESC LIMIT 3) 

所需的輸出

most_requested_item_id | most_requested_item_name | highest_credit_value | highest_credit_user | credit_monthly_user | totalCredit_monthly Descending 1 | monthly_credit_user 
      87    | Poster FLS    | 140     | User A    | User C    | 350        | User D 
      95    | Sample Item 4   | 11     | User B    | User E    | 170        | User F 
      89    | Earth     | 10     | User C    | User G    | 70        | User H 

但是我得到一個錯誤:

The used select statements have a different number of columns union

表的模式,credit_log

Field   | Type   | Null | Key | Default| Extra 
    credit_log_id | int(11)  | NO | PRI | NULL | auto_increment 
    credit_user_id| varchar(255) | YES |  | NULL | 
    credit_date | datetime  | YES |  | NULL | 
    credit_amount | int(11)  | YES |  | NULL | 
    credit_type | varchar(255) | YES |  | NULL | 
    credit_desc | varchar(255) | YES |  | NULL | 

架構表請求

Field    | Type   | Null | Key | Default   | Extra 
    request_id   | int(11)  | NO | PRI | NULL    | auto_increment 
    request_requser_id | varchar(255) | NO | PRI 
    request_item_id  | varchar(255) | NO | PRI 
    request_status  | varchar(20) | NO |  | Active 
    request_lastmodified | datetime  | NO |  | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP 
    request_message  | varchar(225) | YES |  | NULL 

表的模式,項目

Field     | Type   | Null | Key  | Default   | Extra 
    item_id     | int(11)  | NO | PRI  | NULL    | auto_increment 
    item_name    | varchar(255) | NO |   | NULL 
    item_category   | varchar(255) | NO |   | NULL 
    item_desc    | varchar(255) | YES |   | NULL 
    item_user_id   | varchar(255) | YES |   | NULL 
    item_lease_value  | int(11)  | YES |   | NULL 
    item_lease_term   | varchar(255) | YES |   | NULL 
    item_image    | mediumtext | YES |   | NULL 
    item_primary_image_link | varchar(255) | YES |   | NULL 
    item_status    | varchar(255) | NO | Created | 
    item_uid    | varchar(255) | YES |   | NULL 
    item_lat    | float(10,6) | YES |   | NULL 
    item_lng    | float(10,6) | YES |   | NULL 
    item_lastmodified  | datetime  | NO    | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP 

架構表用戶的。

users_table

是否有上述3次的查詢合併爲一個,以便它返回總7列的任何方式??請幫助

+0

見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very -simple-sql-query – Strawberry

+1

你能發表表格模式和期望結果嗎? –

+0

雖然不是必需的,但發佈指向SQL Fiddle(http://sqlfiddle.com)的鏈接對於這樣的問題可能非常有利。 – Jhecht

回答

1

考慮行號或秩加入所有查詢。由於它們都是不相關的部分,我們可以根據他們將分享的行位來匹配。下面將查詢作爲在外部主查詢中連接在一起的派生表運行。

具體地,首先兩個派生表使用相關計子查詢而最後使用定義的變量(如最後更復雜,因爲排序上不同於其它的聚合值運行)。如果計數聚合不起作用,則對所有計數使用變量。而LEFT JOIN超過INNER JOIN在隊伍的情況下,使用不匹配的跨越,並與空值的行會出現在那裏你可以相應地調整級別計算。現在,每個查詢排名輸出。關係也可能是一個問題。

SELECT main1.*, main2.*, main3.* 
FROM 
    (SELECT r.request_item_id AS most_requested_item_id, i.item_name AS most_requested_item_name, 
      (SELECT Count(*) FROM `requests` sub 
      WHERE sub.request_item_id >= r.request_item_id) AS rank 
    FROM `requests` r 
    LEFT JOIN `items` i ON r.request_item_id = i.item_id 
    GROUP BY r.request_item_id, i.item_name 
    ORDER BY COUNT(*) DESC 
    LIMIT 3) main1 

LEFT JOIN 
    (SELECT u.user_credit AS highest_credit_value, u.user_full_name AS highest_credit_user,   
      (SELECT Count(*) FROM `users` sub 
      WHERE sub.user_credit >= u.user_credit) AS rank 
    FROM `users` u 
    ORDER BY u.user_credit DESC 
    LIMIT 3) main2 
ON main1.rank = main2.rank 

LEFT JOIN 
    (SELECT c.credit_user_id AS credit_monthly_user, SUM(c.credit_amount) AS totalCredit_monthly, 
      u.user_full_name AS monthly_credit_user, 
      (@rownum:= @rownum + 1) AS rank 
    FROM `credit_log` c, (select @rownum := 0) sqlvars, 
    LEFT JOIN `users` u ON c.credit_user_id = u.user_id 
    WHERE c.credit_date BETWEEN (CURDATE() - INTERVAL 30 DAY) AND CURDATE() 
    GROUP BY c.credit_user_id, u.user_full_name 
    ORDER BY totalCredit_monthly DESC 
    LIMIT 3) main3 
ON main1.rank = main3.rank