2011-02-15 56 views
2

我在單獨的查詢中查詢兩個表。這樣做會在不到一秒的時間內產生正確的結果,但是,如果我嘗試合併它們,則需要很長時間才能殺死查詢(大量記錄)。我想知道我做錯了什麼。MySQL組合表錯誤查詢

所需的輸出

+--------+-----------+-----------+-----------+  
| name | computers | printers | tvs  | 
+--------+-----------+-----------+-----------+ 
| Peter | 12  | 1  | 5  | 
| Josh | 20  | 4  |   | 
| Bob | 15  | 4  | 9  | 
+--------+-----------+-----------+-----------+ 

查詢1

SELECT 
    names.name, 
    SUM(IF(pcs.electronic = 'computers', 1,0)) AS computers, 
    SUM(IF(pcs.electronic = 'printers', 1,0)) AS printers 
FROM 
    electronics.pcs LEFT JOIN electronics.orders 
    ON pcs.id = orders.id 
    LEFT JOIN electronics.buying 
    ON orders.id = buying.id 
    LEFT JOIN dbnames.names names 
    ON names.clientid = pcs.clientid 
WHERE 
    pcs.clientid IS NOT NULL AND 
    pcs.belongs IN (10) 
GROUP BY pcs.clientid 

表1 - 查詢1

+--------+-----------+-----------+ 
| name | computers | printers | 
+--------+-----------+-----------+ 
| Peter | 12  | 1  | 
| Josh | 20  | 4  | 
| Bob | 15  | 4  | 
+--------+-----------+-----------+ 

查詢2

SELECT 
    names.name, 
    SUM(IF(tvs.electronic = 'tvs', 1,0)) AS tvs 
FROM 
    electronics.tvs LEFT JOIN dbnames.names names 
    ON names.clientid = tvs.clientid 
WHERE 
    tvs.belongs IN (10) 
GROUP BY tvs.clientid 

表2 - 查詢2

+--------+-----------+ 
| name | tvs  | 
+--------+-----------+ 
| Peter | 5  | 
| Bob | 9  | 
+--------+-----------+ 
+0

大,謝謝大家! – Mike79 2011-02-15 05:18:27

回答

0

爲什麼不能做一個簡單的UNION

SELECT 
    names.name, 
    SUM(IF(pcs.electronic = 'computers', 1,0)) AS computers, 
    SUM(IF(pcs.electronic = 'printers', 1,0)) AS printers 
FROM 
    electronics.pcs LEFT JOIN electronics.orders 
    ON pcs.id = orders.id 
    LEFT JOIN electronics.buying 
    ON orders.id = buying.id 
    LEFT JOIN dbnames.names names 
    ON names.clientid = pcs.clientid 
WHERE 
    pcs.clientid IS NOT NULL AND 
    pcs.belongs IN (10) 
GROUP BY pcs.clientid 

UNION ALL 

SELECT 
    names.name, 
    SUM(IF(tvs.electronic = 'tvs', 1,0)) AS tvs 
FROM 
    electronics.tvs LEFT JOIN dbnames.names names 
    ON names.clientid = tvs.clientid 
WHERE 
    tvs.belongs IN (10) 
GROUP BY tvs.clientid 

只是一個想法...

+0

我想知道,你不需要一個平等的專欄所有列?也將結合記錄的結果集,但我需要的是額外的列。 – Mike79 2011-02-15 03:09:03

+0

你對我的道歉是正確的,我正在考慮@cyberkiwi,只是出錯了:)。你可能需要一種`FULL JOIN`或'FULL OUTER JOIN`,就像他提到的那樣,而不是`UNION`。無論如何感謝 – Eli 2011-02-15 03:30:07

0

一個完整的加盟將很好地工作,但是因爲MySQL不支持全加入..你可以UNION ALL兩者之間,並從對應的列坐MAX 。

SELECT clientid, name, 
    MAX(computers) computers, 
    MAX(printers) printers, 
    MAX(tvs) tvs 
FROM 
(
    SELECT 
     pcs.clientid, 
     names.name, 
     SUM(IF(pcs.electronic = 'computers', 1,0)) AS computers, 
     SUM(IF(pcs.electronic = 'printers', 1,0)) AS printers, 
     NULL 
    FROM 
     electronics.pcs LEFT JOIN electronics.orders 
     ON pcs.id = orders.id 
     LEFT JOIN electronics.buying 
     ON orders.id = buying.id 
     LEFT JOIN dbnames.names names 
     ON names.clientid = pcs.clientid 
    WHERE 
     pcs.clientid IS NOT NULL AND 
     pcs.belongs IN (10) 
    GROUP BY pcs.clientid, names.name 
    UNION ALL 
    SELECT 
     tvs.clientid, 
     names.name, 
     NULL, NULL, 
     SUM(IF(tvs.electronic = 'tvs', 1,0)) AS tvs 
    FROM 
     electronics.tvs LEFT JOIN dbnames.names names 
     ON names.clientid = tvs.clientid 
    WHERE 
     tvs.belongs IN (10) 
    GROUP BY tvs.clientid, names.name 
) Q2 
GROUP BY clientid, name 
0
select * 
from 
(
    ... first query 
) as query1 
left join 
(
    ... second query 
) as query2 
on query1.name=query2.name;