我在單獨的查詢中查詢兩個表。這樣做會在不到一秒的時間內產生正確的結果,但是,如果我嘗試合併它們,則需要很長時間才能殺死查詢(大量記錄)。我想知道我做錯了什麼。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 |
+--------+-----------+
大,謝謝大家! – Mike79 2011-02-15 05:18:27