2011-08-16 68 views
0

我有6個供應商的「庫存」數據庫,我已匹配/不匹配主數據庫。如果在主廣告資源中找到唯一ID,我們會在名爲「狀態」的一列中添加「匹配」字樣。如果主人沒有匹配的唯一ID,我們在狀態欄中輸入'不匹配'。我們爲每個供應商庫存數據庫完成了這項工作我想保持一個計數的唯一ID的數量有'匹配'和'不匹配'來快速瀏覽一下。在MySQL中的多個表中計算匹配值

例如:

結果,結合所有6個庫存數據庫:
庫存總量:20000 '匹配' 總:14000 '不匹配' 總:6000

回答

0

這可能不是最好的方式,也沒有經過測試,但這是我想出的。


SELECT SUM(1) AS inventory_total, 
    SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS match_total, 
    SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS no_match_total 
FROM tbl 
WHERE tbl_id IN (
    SELECT MAX(tbl_id) 
    FROM tbl 
    GROUP BY fkey_id 
); 

我不完全確定,如果這就是你所追求的或不,但我希望它有幫助。

0

這項工作?

SELECT 
    COUNT(*) AS InventoryTotal, 
    SUM(CASE WHEN STATUS = 'Match' THEN 1 ELSE 0 END) AS MatchTotal, 
    SUM(CASE WHEN STATUS = 'No Match' THEN 1 ELSE 0 END) AS NoMatchTotal 
FROM 
    Table 
0
SELECT COUNT(*) as [inventory total], 
    SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS [match total] 
    SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS [no match total] 
FROM db1.table UNION ALL db2.table UNION ALL db3.table 
       UNION ALL db4.table UNION ALL db5.table UNION ALL db6.table 

如果我理解正確...