2013-10-28 50 views
1

我有如下表:MySQL的:總和的情況下有多個表

表1:

CREATE TABLE table1 
    (
    id int auto_increment primary key, 
    person varchar(30), 
    color_code varchar(30), 
    item_id varchar(30), 
    date_start DATE, 
    date_complete DATE 
    ); 

INSERT INTO table1 
(person, color_code, item_id, date_start, date_complete) 
VALUES 
('Jackson', 'RED', '1', '2013-07-11 11:23:39', '2013-08-1 11:23:39'), 
('Danny', 'BLUE', '2', '2013-07-20 11:23:39', '2013-08-1 11:23:39'), 
('Jimmy', 'GREEN', '5', '2013-05-15 11:23:39', '2013-08-1 11:23:39'), 
('Jackson', 'RED', '3', '2013-02-16 11:23:39', '2013-08-1 11:23:39'), 
('Jimmy', 'BLUE', '4', '2013-03-13 11:23:39', '2013-08-1 11:23:39'), 
('William', 'RED', '5', '2013-04-29 11:23:39', '2013-08-1 11:23:39'), 
('William', 'BLUE', '1', '2013-05-9 11:23:39', '2013-08-1 11:23:39'), 
('Danny', 'GREEN', '5', '2013-01-6 11:23:39', '2013-08-1 11:23:39'), 
('Jackson', 'RED', '4', '2013-07-11 11:23:39', '2013-08-1 11:23:39'), 
('Jackson', 'RED', '5', '2013-08-7 11:23:39', '2013-09-1 11:23:39'); 

表2:

CREATE TABLE table2 
    (
    id int auto_increment primary key, 
    item_code varchar(30), 
    item_name varchar(30) 
    ); 

INSERT INTO table2 
(item_code, item_name) 
VALUES 
('1', 'APPLE'), 
('2', 'BANANA'), 
('3', 'PINEAPPLE'), 
('4', 'WATERMELON'), 
('5', 'GUAVA'); 

我的SQL查詢看起來像這樣:

SELECT 

    person AS 'Name', 
    COUNT(*) AS 'Total Item Purchased', 
    SUM(CASE WHEN (color_code='RED') THEN 1 ELSE 0 END) AS 'Total Red Color', 
    SUM(CASE WHEN (color_code='BLUE') THEN 1 ELSE 0 END) AS 'Total Blue Color', 
    SUM(CASE WHEN (color_code='GREEN') THEN 1 ELSE 0 END) AS 'Total Green Color', 
    SUM(CASE WHEN DATEDIFF(date_complete, date_start) BETWEEN 1 AND 30 THEN 1 ELSE 0 END) AS 'Bought Between 1-30 Days' 

FROM table1 

    WHERE 
    person LIKE '%Jackson%' OR 
    person LIKE '%Danny%' OR 
    person LIKE '%Jimmy%' OR 
    person LIKE '%William%' 
    GROUP BY person; 

問題:如何顯示item_name表2到所有行組由選擇查詢? IM想着子查詢,後「買間1-30天」這可能嗎?

Sample output that i want

SQLFiddle:http://sqlfiddle.com/#!2/d59e0/4

回答

3

你可以加入表和使用GROUP_CONCAT()

SELECT 

    person AS 'Name', 
    COUNT(*) AS 'Total Item Purchased', 
    SUM(CASE WHEN (color_code='RED') THEN 1 ELSE 0 END) AS 'Total Red Color', 
    SUM(CASE WHEN (color_code='BLUE') THEN 1 ELSE 0 END) AS 'Total Blue Color', 
    SUM(CASE WHEN (color_code='GREEN') THEN 1 ELSE 0 END) AS 'Total Green Color', 
    SUM(CASE WHEN DATEDIFF(date_complete, date_start) BETWEEN 1 AND 30 THEN 1 ELSE 0 END) AS 'Bought Between 1-30 Days', 
    GROUP_CONCAT(item_name) 

FROM table1 JOIN table2 ON table2.item_code = table1.item_id 

    WHERE 
    person LIKE '%Jackson%' OR 
    person LIKE '%Danny%' OR 
    person LIKE '%Jimmy%' OR 
    person LIKE '%William%' 
    GROUP BY person; 

看到它的sqlfiddle

請注意,簡單地加入表格並將結果集排序person,在應用程序的表示層中執行此類聚合有時會更好。

+0

工作般的魅力,怎麼是GROUP_CONCAT工作? – Teddybugs

+0

@大衛:繼鏈路MySQL文檔,「*該函數返回與從一組級聯非空值的字符串結果。*」 – eggyal

+0

是有可能操縱GROUP_CONCAT()來顯示多個字段。例如:我有item_name和item_manufacture,然後我想顯示它: 蘋果 - 澳大利亞 香蕉 - 美國 單獨換行而不是逗號。可能嗎? – Teddybugs

1

你可以加入表

SELECT 

    person AS 'Name', 
    COUNT(*) AS 'Total Item Purchased', 
    SUM(CASE WHEN (color_code='RED') THEN 1 ELSE 0 END) AS 'Total Red Color', 
    SUM(CASE WHEN (color_code='BLUE') THEN 1 ELSE 0 END) AS 'Total Blue Color', 
    SUM(CASE WHEN (color_code='GREEN') THEN 1 ELSE 0 END) AS 'Total Green Color', 
    SUM(CASE WHEN DATEDIFF(date_complete, date_start) BETWEEN 1 AND 30 THEN 1 ELSE 0 END) 'Bought Between 1-30 Days', 
    item_name as 'item' 

FROM table1,table2 

    WHERE 
    table1.item_id = table2.item_code and (
    person LIKE '%Jackson%' OR 
    person LIKE '%Danny%' OR 
    person LIKE '%Jimmy%' OR 
    person LIKE '%William%') 
    GROUP BY person; 

檢查JS http://sqlfiddle.com/#!2/d59e0/13