SQL更新其中的数据集 [英] SQL update where in set of data

查看:157
本文介绍了SQL更新其中的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+------------------+
| id1 | id2 | bool |
+------------------+
|  1  |  1  |  F   |
|  1  |  2  |  F   |
|  2  |  1  |  F   |
+------------------+

UPDATE table_name
SET bool = T
WHERE (id1, id2) IN ((1,1),(2,1)) --Need work here

所以基本上我要选择在哪里(ID1,ID2)=(值1,值2)的条件。
类似下面的语句:

So basically I want to select where the conditions of (id1, id2) = (value1, value2). Similar to the statement below:

WHERE id1 = value1 AND id2 = value2

然而,在阵列中的一组值。
这可能吗?

however in set of values in an array. Is this possible?

在此先感谢

编辑:我使用的是SQL Server 2008中我很抱歉,如果它是不是太清楚。
我试图把这个作为一个存储过程,并从服务调用它。输入是某种数组(可变大小),并找到在连续两个ID匹配。

I'm using SQL server 2008. I'm sorry if it wasn't too clear. I'm trying to put this as a stored procedure and call it from a service. The input would be some sort of an array (variable size), and find a match with the two IDs in a row.

推荐答案

下面是做到在MSSQL的方式。所有你需要的是让一个值(在本例VARCHAR)从为Id1和ID2。在这种情况下,您可以使用与设置的值声明。此外,如果允许他们在这些领域,你应该想想ID1空值和ID2(刚加入:和ID1不为空和ID2不为空)。

Here is the way to do it in MSSql. All you need is to make one value (in this example VARCHAR) from Id1 and Id2. In this case you can use IN statement with the values set. Also you should think about NULLs in id1 and id2 if they are allowed in these fields (just add: and id1 is not null and id2 is not null).

UPDATE table_name
SET bool = T
WHERE convert(varchar(20),id1)+','+convert(varchar(20),id2) in ('1,1','2,1')

这篇关于SQL更新其中的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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