[英]My SQL - return the rolls/ids from a where clause list which is not exist in table?
下面的查询是一个示例查询。 我想从你们那里得到一个想法。 我写了一个查询,它返回那些在表中找到的结果,但我在 where 子句列表中使用了很多卷,但在数据库中找不到所有卷。
SELECT roll,name,phone FROM my_tbl where roll in (1,2,3,4,5,6)
This query gives the result like
Roll Name Phone
2 name1 123
3 name2 345
6 name3 785
That query returns only those result which is stored into the table.
But I want to get all of rolls which is used in clause (1,2,3,4,5,6).
My expecting result should be like
Roll Name Phone
1 n/a 0
2 name1 123
3 name2 345
4 n/a 0
5 n/a 0
6 name3 785
Is it possible ? If possible then how ?
您需要一个包含以下值的派生表:
select *
from (select 1 as roll union all
select 2 as roll union all
. . .
select 6 as roll
) r left join
my_tbl t
using (roll);
这用NULL
填充未知卷的值,这似乎是一个合适的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.