Mysql语句,如果至少有一行包含结果 [英] Mysql statement, if at least one row contains result

查看:95
本文介绍了Mysql语句,如果至少有一行包含结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,我有一张记录表:



 ------------- --------------------- 
ID | UniqueId |名称|结果
----------------------------------
1 1 Test1 OK
2 1 Test1取消
3 1 Test1 OK
4 2 Test2 OK
5 2 Test2 OK
6 2 Test2 OK
7 2 测试2 OK
8 3 Test3 OK
9 3 测试3确定





让我们说我是不检查至少有一行UniqueId = 1不包含Result == Cancelled。

我该怎么做?

谢谢

解决方案

你可以使用这个

 选择 * 来自 TableName 其中 UniqueId =  1   AND 结果<> ' 已取消' 


您想知道是否有任何带有该唯一ID的测试被取消了吗?



好​​吧,如果你使用类似的东西,你可以把它读作布尔值,它将是真的= 1 ,如果你愿意的话,你可以用更多的东西进行改进,然后放入存储过程







声明@tbl表(
id int,
uniqueid int,
name nvarchar(10),
result nvarchar(20)


插入@tbl(id,uniqueid,name,result)
values(1,1,'Test1','OK')
插入@tbl(id) ,uniqueid,name,result)
values(2,1,'Test1','Cancelled')
插入@tbl(id,uniqueid,name,result)
values(4, 2,'Test2','OK')
插入@tbl(id,uniqueid,name,result)
values(5,2,'Test1','OK')


选择a.uniqueid,a.result ='Cancelled'时的情况en 1 else 0 end wascancelled
from
(select unique uniqueid,min(Result)result
from @tbl
group by uniqueid)a


Greetings, i've a table with records:

----------------------------------
ID   |  UniqueId |  Name  | Result
----------------------------------
1       1           Test1   OK
2       1           Test1   Cancelled
3       1           Test1   OK
4       2           Test2   OK
5       2           Test2   OK
6       2           Test2   OK
7       2           Test2   OK
8       3           Test3   OK
9       3           Test3   OK



Let's say i wan't to check if at least one row with UniqueId = 1 not contains Result == Cancelled.
How can i do this?
Thank you

解决方案

You can use this

Select * from TableName Where UniqueId = 1 AND Result <> 'Cancelled'


You want to know if any test with that unique id was cancelled?

Well if you use something like a bit you can read it as boolean and it would be true=1, which yields something you can refine with a where more and put in a stored procedure if you like



declare @tbl table(
	id int,
	uniqueid int,
	name nvarchar(10),
	result nvarchar(20)
)

insert into @tbl(id, uniqueid, name, result)
values(1,1, 'Test1', 'OK')
insert into @tbl(id, uniqueid, name, result)
values(2,1, 'Test1', 'Cancelled')
insert into @tbl(id, uniqueid, name, result)
values(4,2, 'Test2', 'OK')
insert into @tbl(id, uniqueid, name, result)
values(5,2, 'Test1', 'OK')


select a.uniqueid, case when a.result = 'Cancelled' then 1 else 0 end wascancelled
from 
(select distinct uniqueid, min(Result) result
from @tbl
group by uniqueid) a


这篇关于Mysql语句,如果至少有一行包含结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆