Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询结果中不需要的空值_Mysql_Sql - Fatal编程技术网

MySQL查询结果中不需要的空值

MySQL查询结果中不需要的空值,mysql,sql,Mysql,Sql,我有下面的MySQL表 tbl_宠物_类型: +----------+-------------+ | pet | type | +----------+-------------+ | cat | mammal | | dog | mammal | | goldfish | fish | | goldfish | seacreature | | snake | reptile | +----------

我有下面的MySQL表

tbl_宠物_类型:

+----------+-------------+
| 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()函数谢谢您的回答。我对原来的问题做了一些修改。见上文。