table A
id name
1 a
2 c
3 d
select * from A where name in ('a','b','c')
明显 查询结果没有b 但我想要的结果
返回 b
就是查询一堆的数据返回不存在的
id name
1 a
2 c
3 d
select * from A where name in ('a','b','c')
明显 查询结果没有b 但我想要的结果
返回 b
就是查询一堆的数据返回不存在的
select 'b' c
)
select * from m where not exists(select * from t where m.c = t.id)
where name in ('a','b','c')这个 in 后面,有好几万个 值吗?
where name in ('a','b','c')这个 in 后面,有好几万个 值吗?好几千~~
(
select 'a' name union all
select 'b' name union all
select 'c' name
)
select * from B left join A on B.name=A.name
可以参考:use test
go--> --> (Andy)生成測試數據
if not object_ID('Tempdb..#') is null
drop table #
Go
Create table #(id nvarchar(2),name nvarchar(4))
Insert #
select '1','a' union all
select '2','c' union all
select '3','d'
Go
Select char(ascii(b.name)-1) as name
from # as a
inner join # as b on b.name=char(ascii(a.name)-1)/*
name
-----------------------
b
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[f_splitstr](@SourceSql NVARCHAR(MAX),@StrSeprate VARCHAR(100))
RETURNS @temp TABLE(F1 VARCHAR(100))
AS
BEGIN
DECLARE @ch AS VARCHAR(100)
SET @SourceSql=@SourceSql+@StrSeprate
WHILE(@SourceSql<>'')
BEGIN
SET @ch=LEFT(@SourceSql,CHARINDEX(@StrSeprate,@SourceSql,1)-1)
INSERT @temp VALUES(@ch)
SET @SourceSql=STUFF(@SourceSql,1,CHARINDEX(@StrSeprate,@SourceSql,1),'')
END
RETURN
END然后再操作数据:
declare @s varchar(100),@sql varchar(1000)
set @s='a,b,c,d,e,f';
;WITH A(id,name)AS(
select 1,'a' union all
select 2,'c' union all
select 3,'d'
)
SELECT F1
FROM dbo.f_splitstr(@s, ',')
WHERE F1 NOT IN ( SELECT name
FROM A )结果:
还有一个问题 update 100 条数据 返回更新了90条
我想获取没有更新的10条怎么搞
出现这样的情况有可能是你的数据存在重复
正常是以表的数据为主,你这里是以你这堆数据为主。
关键的是你这些数据是以什么数据格式得到的呢?('a','b','c' 怎么来的)
正常的思路
将这些数据放在一个表里,然后类似这样(我随表手写的) select * from (values('a'),('b'),('c')) s(name)
where not exists(select 0 from TableA as a where a.Name=s.[name])