2
臺設備:次數多行的值,MySQL的
id device type_id device_status
1 dev1 2 1
2 dev2 2 2
3 dev3 7 2
4 dev5 9 3
型號表:
model_id vendor_id model_name
1 3 name1
2 5 name2
3 7 name3
4 7 name4
和查詢中查找此:
SELECT model_id, type_id, model_name,
COUNT(type_id) AS numOfDevices
FROM devices
LEFT JOIN models ON models.model_id = devices.type_id
WHERE device_status = 2 OR device_status = 3
GROUP BY type_id
什麼,我從得到該查詢是:
There are $numOfDevices $model_name items.
這是好的,但有沒有一種方法可以在同一個查詢中僅使用device_status = 3對設備進行計數?
'SUM(DEVICE_STATUS = 2)device2'不讓它強壯那麼它是:)的 「布爾」 返回0或1,所以你的CASE WHEN不是必需的。 –
我收到錯誤:{「errorInfo」:[「42000」,1064,「你的SQL語法有錯誤;檢查與你的MySQL服務器版本相對應的手冊,在'FROM devices \ r'附近使用正確的語法\ n \ t \ t \ t \ t \ t \ t \ t在第4行添加模型ON models.model_id = devices.type_id \ r'「}}。順便說一下,我使用的PDO和5.1.62是MySQL的版本 – InTry
刪除最後一個','在第三行。 –