in查询结果不存在的时候返回特定的结果

MySql 码拜 8年前 (2016-02-15) 1648次浏览
例如:
select * from shop where shop_id in (1,2,3)
其中存在的结果只有shop_id为1,2
例如:
id   name
1    商店1
2   商店2
有没有方法可以获得
id   name
1    商店1
2    商店2
3    默认值
这样子呢?
解决方案

10

在前端程序拼接更好

30

让程序去组装数据,
sql也可以写,比较麻烦

select a.id,ifnull(b.`name`,"默认值") from
(
	select 1 as id
	union all
	select 2 as id
	union all
	select 3 as id
)a left join shop b on a.id = b.shop_id

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明in查询结果不存在的时候返回特定的结果
喜欢 (0)
[1034331897@qq.com]
分享 (0)