我有以下MySQL表不需要NULL值導致
tbl_pet_types:
+----------+-------------+
| pet | type |
+----------+-------------+
| cat | mammal |
| dog | mammal |
| goldfish | fish |
| goldfish | seacreature |
| snake | reptile |
+----------+-------------+
我有這樣的查詢選擇一個青蛙和它的類型:
SELECT types.pet,
group_concat(types.type separator ', ') AS type
FROM tbl_pet_types types
WHERE types.pet IN ('frog', 'mouse', 'goldfish');
由於青蛙和鼠標不是tbl_pet_types,返回的結果是:
+----------+-------------------+
| pet | type |
+----------+-------------------+
| goldfish | fish, seacreature |
+----------+-------------------+
但我想:
+----------+------------------+
| pet | type |
+==========+==================+
| frog | NULL |
+----------+------------------+
| mouse | NULL |
+----------+------------------+
| goldfish | fish,seacreature |
+----------+------------------+
我怎樣才能改變我的查詢得到我想要的結果嗎?
嘗試IFNULL()函數 – money