喜欢和不喜欢同一列

时间:2015-04-03 09:40:45

标签: sql sql-server tsql

我正在尝试在Sql server mgmt studio上运行此查询,其中Like和not Like语句将在同一查询的同一列上使用 我想知道发送了最终提醒通知的所有记录,但没有为所有记录发送NTK信件。它给我的结果与----相似'%Final Reminder%&# 39;

select [rjvn_pound_reference],[rjvn_time],[rjvn_userid] ,[rjvn_note] FROM [ICPS].[dbo].[removal_job_vehicle_notes] where removal_job_vehicle_notes.rjvn_note  not like '%NTK%'

select  [rjvn_pound_reference],[rjvn_time],[rjvn_userid],[rjvn_note]  
FROM [ICPS].[dbo].[removal_job_vehicle_notes] where removal_job_vehicle_notes.rjvn_note   like '%Final Reminder%'

这是我的疑问:

select  [rjvn_pound_reference],[rjvn_time],[rjvn_userid],[rjvn_note]  
    FROM [ICPS].[dbo].[removal_job_vehicle_notes] where
 removal_job_vehicle_notes.rjvn_note   like '%Final Reminder%' and 
  removal_job_vehicle_notes.rjvn_note  not like '%NTK%

数据:

rjvn_pound_reference rjvn_time rjvn_userid rjvn_note

1109225 24/03/2015 11:23:01 JEMMAT  Drive off final reminder sent
1110236 12/03/2015 10:20:03 KATIEB  Drive off- Final reminder sent
1110359 25/03/2015 13:07:39 KATIEB  Drive off- Final reminder notice 
1110996 20/03/2015 09:11:09 JEMMAT  Drive off final reminder sent

2 个答案:

答案 0 :(得分:1)

好像你要检查多行。然后你需要分成两个由NOT EXISTS连接的查询,例如如果你想检查每个[rjvn_userid]

select  [rjvn_pound_reference],[rjvn_time],[rjvn_userid],[rjvn_note]  
FROM [ICPS].[dbo].[removal_job_vehicle_notes] as t1 
where removal_job_vehicle_notes.rjvn_note  like '%Final Reminder%' 
and not exists
 ( 
   select * from [ICPS].[dbo].[removal_job_vehicle_notes] as t2
   where t1.[rjvn_userid] = t2.[rjvn_userid]
   and removal_job_vehicle_notes.rjvn_note like '%NTK%
 )

答案 1 :(得分:-1)

where
    (removal_job_vehicle_notes.rjvn_note like '%Final Reminder%')
    and not (removal_job_vehicle_notes.rjvn_note like '%NTK%')