简体   繁体   English

如何从MySQL中的另一个选择结果中进行选择?

[英]How can I select from the results of another select in MySQL?

I have a query to select all records from a table. 我有一个查询要从表中选择所有记录。 There is a status field, so each record has a particular status. 有一个状态字段,因此每个记录都有一个特定的状态。 I want to select all records regardless of the status. 我想选择所有记录,无论其状态如何。 There is another field called time however, I only want to select this field if the status = "A". 还有一个称为时间的字段,但是我只想在状态=“ A”时选择此字段。

So the below query will show all the data 所以下面的查询将显示所有数据

Select field1, field2, field3, status, time from table1

However, I need the query to be more like this: 但是,我需要查询更像这样:

Select field1, field2, field3, status, time(where status = "A") from table1

I want to see all other records regardless of the status however, I want NULL or black in the time field where the status is A. 我想查看所有其他记录,而不论其状态如何,但是我希望在状态为A的时间字段中为NULL或黑色。

Couple options, here's a generic approach with case : 几个选项,这是一个带有case的通用方法:

Select field1, field2, field3, status, 
    case when status = 'A' then 'black' end as `time`
from table1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM