Mysql 如何从SQL查询(包括子查询)中选择全部
我想执行一个mySQL查询,返回一个表中的所有结果,但如果它们对应于一个单独的查询,则有一个额外的列,该列的值为“selected”。比如说Mysql 如何从SQL查询(包括子查询)中选择全部,mysql,sql,Mysql,Sql,我想执行一个mySQL查询,返回一个表中的所有结果,但如果它们对应于一个单独的查询,则有一个额外的列,该列的值为“selected”。比如说 | name | |------| | a | | b | | c | 我希望在执行查询“select*FROMTABLE where name='a'”时能够返回以下内容 | name | selected | |------|----------| | a | yes | | b | | |
| name |
|------|
| a |
| b |
| c |
我希望在执行查询“select*FROMTABLE where name='a'”时能够返回以下内容
| name | selected |
|------|----------|
| a | yes |
| b | |
| c | |
也就是说,我还想知道哪些行被遗漏了。where子句限制您的行集,这样您就只能得到名为“a”的行。 要获得所有这些,请将受限表连接回其自身,或者使用IF-without-where:
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
+1给出给定SQL的最佳答案,但是OP的实际查询可能更复杂
select a.*
, name = case
when
a.name = 'a'
then 0
else 1
end
as selected
from points as a