给定表格:
id domain name value
----------------------------------
1 NULL a v1
2 example.com a v2
3 example.net a v3
4 NULL b v4
5 example.net b v5
6 example.com c v6
7 example.com d v7
8 NULL d v8
如何运行一个查询来检索域 =“example.com”的以下结果?请注意,当未为域指定值时,假定为 NULL
域。
name value
--------------
a v2
b v4
c v6
d v7
对于域 = "example.net":
name value
--------------
a v3
b v5
d v8
最佳答案
像这样
(select name, value
from <your_table_name>
where domain = 'example.com')
union
(select name, value
from <your_table_name>
where domain is null
and name not in
(select name
from <your_table_name>
where domain = 'example.com'))
关于mysql - 从表中选择行但尽可能检索覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6764824/