我有一个这样的数据表:
-------------+-------------+-------
| foo_date | description | flag |
-------------+-------------+-------
| 2014-01-01 | asdf 123 | 1 |
-------------+-------------+-------
| 2014-01-01 | FOO | 1 |
-------------+-------------+-------
| 2014-01-02 | asdf 456 | 1 |
-------------+-------------+-------
我试图将任何行的标志设置为0,其中描述不是' t" FOO"并且在同一天有一排是" FOO"。因此,在此示例数据集中,第一行将获得flag = 0,因为同一日期存在FOO,但第三行不会因为该日期没有匹配的FOO。这就是我提出的有效的方法,但它并不是最有效的方法。
UPDATE
foo_table t1
JOIN
(
SELECT
*
FROM
foo_table
WHERE
(foo_date) IN
(
SELECT
foo_date
FROM
foo_table
WHERE
description LIKE 'FOO%'
)
AND description NOT LIKE 'FOO%'
) AS t2
ON
(
t1.foo_date = t2.foo_date
AND t1.description = t2.description
)
SET
t1.flag = 0
答案 0 :(得分:0)
update t1
set t1.flag = 0
from foo_table t1
where t1.description <> 'FOO'
and exists (select *
from foo_table t2
where t2.foo_date=t1.foo_date
and t2.description = 'FOO')
答案 1 :(得分:0)
根据您的数据,这可能会更有效:
update t1
set t1.flag = 0
from foo_table t1
where isnull(t1.description,'') <> 'FOO'
and t1.foo_date in (select t2.foo_date
from foo_table t2
where t2.description = 'FOO')