問題:我無法讓我的數據庫返回數據庫中存在的location_id的0/NULL,但沒有在tbl_inventory中爲它們分配任何項目,除了已將項目分配給它們的location_id的計數外。MySQL計數不會返回空數與我的其餘數據。
tbl_inventory:(只發布pertainent列)
location_id, shop_id, ...
1 | 100
1 | 100
3 | 106
4 | 100
10 | 102
2 | 100
tbl_location:(發佈所有列)
location_id, location_desc, shop_id
1 | Shop Stock | 100
2 | Crash Kit | 100
3 | Site 4 | 106
4 | Radar Site | 102
5 | New Location | 100
...
10 | Fire Stn 2 | 100
查詢
SELECT
tbl_location.location_desc,
tbl_inventory.location_id,
COALESCE(COUNT(tbl_location.location_id), 0) as 'idCnt'
FROM tbl_location
LEFT OUTER JOIN tbl_inventory
ON tbl_location.location_id = tbl_inventory.location_id
WHERE tbl_inventory.shop_id = 110
GROUP BY tbl_location.location_id;
個
結果
location_desc, location_id, idCnt
Shop Stock | 1 | 2
Crash Kit | 2 | 1
Fire stn 2 | 10 | 1
這到底是怎麼失蹤從tbl_location LOCATION_ID 5
New Location | 5 | 0 (or Null, because there are none in tbl_inventory)
說明:
我tbl_location是分配容器標籤的表(和創建)在我們的網絡中的個別商店。然後該商店擁有該容器的所有權,並可以爲其分配零件(tbl_inventory.location_id)以用於庫存目的。
我的PHP應用程序使用此查詢根據項目數量生成可用位置和授予選項的列表分配給它..如果項目的數量是0,我希望能夠刪除該位置。
更慘
如果我要刪除GROUP BY整個查詢被打亂了.. 只有返回1個LOCATION_ID /遞減,並在該行的整個總總結。
location_desc, location_id, idCnt
Shop Stock | 1 | 5
我不知道什麼是真正回事(在更糟糕的塊) - 但是我手工計數和原來的查詢返回正確的計數我只是不能讓它描繪位置實際上,DO存在於數據庫中,但實際上沒有分配給它們的任何項目。
任何人有任何見解?我發現的所有修復都還沒有工作。