MySQL查询结果中不需要的空值
我有下面的MySQL表 tbl_宠物_类型:MySQL查询结果中不需要的空值,mysql,sql,Mysql,Sql,我有下面的MySQL表 tbl_宠物_类型: +----------+-------------+ | pet | type | +----------+-------------+ | cat | mammal | | dog | mammal | | goldfish | fish | | goldfish | seacreature | | snake | reptile | +----------
+----------+-------------+
| 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 | type |
+----------+-------------------+
| goldfish | fish, seacreature |
+----------+-------------------+
但我想:
+----------+------------------+
| pet | type |
+==========+==================+
| frog | NULL |
+----------+------------------+
| mouse | NULL |
+----------+------------------+
| goldfish | fish,seacreature |
+----------+------------------+
如何更改查询以获得所需的结果?试试这个,因为frog不会出现在tbl\u pet\u类型中,您需要
将其插入表中
INSERT INTO tbl_pet_types (pet, type)
VALUES ('frog', NULL);
然后重新运行查询如果类型。pet
为空
,则使用该函数指定默认值:
SELECT COALESCE(types.pet, 'frog'),
GROUP_CONCAT(types.type separator ', ') AS type
FROM tbl_pet_types types
WHERE types.pet='frog';
在SELECT查询检查之后,必须在tbl_pet_类型(pet,type)值中插入('frog',NULL)
选择仅检索已设置到数据库中的数据
您可以在此处查看更多信息:尝试IFNULL()函数谢谢您的回答。我对原来的问题做了一些修改。见上文。