如何使用SQL查询管理同一个表的多个状态。我有两个GridView来显示EvaluationStatus和CertificateStatus的数据库。
EvaluationStatus的默认值为Not Assigned
,CertificateStatus为Awaited
。
在第一个网格视图中,使用以下查询正确显示Cleared
用户的数据。
select * from MyTable where EvaluationStatus='Cleared' and CertificateStatus='Cleared'
在Second Gridview中,没有显示正确的数据来显示未被清除的用户,但不知道如何编写该查询,下面写了一个查询,但无法正常工作。
select * from MyTable where EvaluationStatus in('Not Assigned','Cleared') and CertificateStatus='Awaited'
第二个gridview查询用户是否将上传证书意味着状态将从Awaited
更改为cleared
。然后上面的查询不起作用,因为现在表中的EvaluationStatus="未分配",CertificateStatus="已清除"。
您可以使用case语句使用条件where子句-
select * from MyTable where 1 = case when EvaluationStatus = 'Not Assigned' and CertificateStatus='Awaited' then 1
when EvaluationStatus = 'Cleared' and CertificateStatus='Awaited' then 1
when EvaluationStatus = 'Not Assigned' and CertificateStatus='Cleared' then 1
else 0 end
我们也可以通过下面的查询来完成。
select *
from MyTable
where RegistrationId in (select RegistrationId from MyTable where EvaluationStatus in('Not Assigned','Cleared') and CertificateStatus='Awaited')
or RegistrationId in (select RegistrationId from MyTable where EvaluationStatus in('Not Assigned') and CertificateStatus in('Awaited','Cleared'))