[英]How to select all from SQL query including subquery
我想执行一个mySQL查询,该查询返回表中的所有结果,但是如果它们对应于一个单独的查询,则具有一个附加列,该列具有“选定”值。 例如
| name |
|------|
| a |
| b |
| c |
当我执行查询“从name ='a'的表中选择*时,我希望能够返回以下内容:
| name | selected |
|------|----------|
| a | yes |
| b | |
| c | |
就是说,我也想知道哪些遗漏了。
select *
, selected = case
when exists (
select *
from table2 t2
where t2.field = t1.field
and ...etc.
) then 1
else 0
end
from table t1
Where子句限制行集,因此您只能获得名称为“ a”的行。 要使所有这些表都可以将受限表重新加入自身或使用IF,而无需在哪里:
SELECT *, IF( name='a', 'yes', '') AS selected
FROM table
对我有用的是:
select a.*
, name = case
when
a.name = 'a'
then 0
else 1
end
as selected
from points as a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.