Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何考虑查询中的条件?_Sql_Postgresql - Fatal编程技术网

Sql 如何考虑查询中的条件?

Sql 如何考虑查询中的条件?,sql,postgresql,Sql,Postgresql,我有一个问题 select student, case when student_color is '***' then 0 else 1 end from student_table where student_color = 'red' 我想把学生的颜色考虑在什么情况下。 如何实现从where条件准备的case语句?where子句仅过滤记录。如果你使用where子句,你将永远不会得到任何其他颜色。Case将为您提供值(1)或1。我想你可能需要在下面- select student

我有一个问题

select student, case when student_color is '***' then 0 else 1 end
from student_table
where student_color = 'red'
我想把学生的颜色考虑在什么情况下。
如何实现从where条件准备的case语句?

where子句仅过滤记录。如果你使用where子句,你将永远不会得到任何其他颜色。Case将为您提供值(1)或1。我想你可能需要在下面-

select student
      ,case when student_color = 'red' then 0 else 1 end as colour
from student_table

您可以使用以下命令更改查询

select student, 
    case 
    when student_color = 'red' 
    then 0 else 1 end
    from student_table
    where student_color = 'red'

添加一些示例表数据和预期结果-作为格式化文本,而不是图像。我无法在答复中显示确切的表示形式。当条件满足时,我希望所有结果都用0和1分开。你好,托布,为了理解,我添加了where。。。。我不想对数据进行过滤。。我想用提供的子句来分隔数据。您应该已经阅读和。但是这应该是
student\u color='red'