开发者

Query to delete records

I have a table with the following fields. My requirement is to delete the records from the table when 1. The Flag is set to 'No' 2. For a title, in one record if the flag is set to 'No' and in another record if it is set to 'Yes'. Delete both the records. For example TH-123, I开发者_StackOverflown 1st record the flag is set to 'No' and in 4th record flag is set 'Yes' Delete both the records. 3. For a title, in one record if the flag is set to 'No' and in another record if it is set to ''(space). Delete both the records. For example TH-344. Delete both the records.

   Title                        Flag     

    TH-123                        No
    TH-344                        
    TH-543                        No
    TH-123                        Yes
    Th-344                        No
    Th-098                        Yes
    Th-876                        Yes
    Th-098                        Yes
    Th-512                        No

so now i am expecting a result, with the following records.

    Th-098                        Yes
    Th-876                        Yes
    Th-098                        Yes

I have done this with 2 queries.

update temp1 set [flag] = 'No' WHERE Title IN 
(SELECT Title FROM temp1 where [flag] = 'No')

delete from temp1 where [flag] = 'No'

Is it possible to do with a single query. If so please let me know or any other better procedure..


delete from temp1 
where 
    Title IN 
        (SELECT Title FROM temp1 where [flag] = 'No')


This should works:

delete from t1
from temp1 t1
  inner join temp2 t2 on t1.Title = t2.Title
where t2.[flag] = 'No'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜