数据:
SELECT DISTINCT A2,A8 FROM YHJ_TEMPFFGG73 WHERE A8 LIKE '%135S/4%'
A2 A8
8880309 135S/4
8880310 135S/4
8880311 135S/4
SELECT * FROM xtm124 WHERE wplbdm='07' AND wpsxmc LIKE '%135S/4%'
wplbdm wplbzb wpsxdm wpsxmc wpsxms wpsjsx
07 00 07361 80S/4+135S/4 NULL NULL
需要找出 在 YHJ_TEMPFFGG73.A8中而不在xtm124.wpsxmc的值,并列出来:
方法一:
SELECT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' )
返回0行,我的理解这里应该能返回一些行数的,起码135S/4,应该能返回来。为什么呢?
而方法二:
SELECT DISTINCT LTRIM(RTRIM(A8)) FROM YHJ_TEMPFFGG73 WHERE NOT EXISTS
(SELECT 1 FROM xtm124 WHERE LTRIM(RTRIM(A8))=LTRIM(RTRIM(wpsxmc)) AND wplbdm='07')
却返回30行左右。
我做了如下测试:
CREATE TABLE YHJA
( A1 NVARCHAR(20))
INSERT YHJA VALUES('Y')
CREATE TABLE YHJB
( A1 NVARCHAR(20))
INSERT YHJB VALUES('YY')
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB)
返回:
A1
Y
这似乎证明“方法一”应该是有效的。
6 个解决方案
#1
用楼主的数据测试了一下,方法一是有返回值的。表数据库多吗?不多都贴上来,让大家试一下
--测试数据
if not object_id(N'Tempdb..#YHJ_TEMPFFGG73') is null
drop table #YHJ_TEMPFFGG73
Go
Create table #YHJ_TEMPFFGG73([A2] int,[A8] nvarchar(26))
Insert #YHJ_TEMPFFGG73
select 8880309,N'135S/4' union all
select 8880310,N'135S/4' union all
select 8880311,N'135S/4'
GO
if not object_id(N'Tempdb..#xtm124') is null
drop table #xtm124
Go
Create table #xtm124([wplbdm] nvarchar(22),[wplbzb] nvarchar(22),[wpsxdm] nvarchar(25),[wpsxmc] nvarchar(32),[wpsxms] nvarchar(21),[wpsjsx] nvarchar(21))
Insert #xtm124
select N'07',N'00',N'07361',N'80S/4+135S/4',NULL,NULL
Go
--测试数据结束
SELECT A8
FROM #YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM #xtm124
WHERE wplbdm = '07' )
----------------
SELECT DISTINCT LTRIM(RTRIM(A8)) FROM #YHJ_TEMPFFGG73 WHERE NOT EXISTS
(SELECT 1 FROM #xtm124 WHERE LTRIM(RTRIM(A8))=LTRIM(RTRIM(wpsxmc)) AND wplbdm='07')
#2
数据也不少,我用版主的,也能返回值,但是放到真实数据里,没有返回值,怪事。
#3
把表数据导入到一张新表上试试,可能和索引有关系
#4
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB)
加一个 is not null,这个是典型的null问题。
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB where A1 is not null)
加一个 is not null,这个是典型的null问题。
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB where A1 is not null)
#5
依sinat_28984567建议:
-- 重新建立表
SELECT LTRIM(RTRIM(wpsxmc)) wpsxmc
INTO YHJTABLEAA
FROM xtm124
WHERE wplbdm = '07'
--重建索引
ALTER INDEX ALL ON YHJTABLEAA REBUILD;
ALTER INDEX ALL ON YHJ_TEMPFFGG73 REBUILD;
ALTER INDEX ALL ON xtm124 REBUILD;
SELECT *
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT wpsxmc
FROM YHJTABLEAA
)
SELECT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' )
结果还是不理想,我想是不是排序规则的问题?
YHJ_TEMPFFGG73 Chinese_PRC_CS_AS_WS
xtm124 Chinese_PRC_CS_AS_WS
结果二者一样,奇怪了。
-- 重新建立表
SELECT LTRIM(RTRIM(wpsxmc)) wpsxmc
INTO YHJTABLEAA
FROM xtm124
WHERE wplbdm = '07'
--重建索引
ALTER INDEX ALL ON YHJTABLEAA REBUILD;
ALTER INDEX ALL ON YHJ_TEMPFFGG73 REBUILD;
ALTER INDEX ALL ON xtm124 REBUILD;
SELECT *
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT wpsxmc
FROM YHJTABLEAA
)
SELECT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' )
结果还是不理想,我想是不是排序规则的问题?
YHJ_TEMPFFGG73 Chinese_PRC_CS_AS_WS
xtm124 Chinese_PRC_CS_AS_WS
结果二者一样,奇怪了。
#6
确实像 wmxcn2000所说的NULL 问题:
SELECT DISTINCT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT DISTINCT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' AND wpsxmc IS NOT NULL )
-- 增加了 AND wpsxmc IS NOT NULL 就有结果了,又学习一招,非常感谢。
SELECT DISTINCT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT DISTINCT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' AND wpsxmc IS NOT NULL )
-- 增加了 AND wpsxmc IS NOT NULL 就有结果了,又学习一招,非常感谢。
#1
用楼主的数据测试了一下,方法一是有返回值的。表数据库多吗?不多都贴上来,让大家试一下
--测试数据
if not object_id(N'Tempdb..#YHJ_TEMPFFGG73') is null
drop table #YHJ_TEMPFFGG73
Go
Create table #YHJ_TEMPFFGG73([A2] int,[A8] nvarchar(26))
Insert #YHJ_TEMPFFGG73
select 8880309,N'135S/4' union all
select 8880310,N'135S/4' union all
select 8880311,N'135S/4'
GO
if not object_id(N'Tempdb..#xtm124') is null
drop table #xtm124
Go
Create table #xtm124([wplbdm] nvarchar(22),[wplbzb] nvarchar(22),[wpsxdm] nvarchar(25),[wpsxmc] nvarchar(32),[wpsxms] nvarchar(21),[wpsjsx] nvarchar(21))
Insert #xtm124
select N'07',N'00',N'07361',N'80S/4+135S/4',NULL,NULL
Go
--测试数据结束
SELECT A8
FROM #YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM #xtm124
WHERE wplbdm = '07' )
----------------
SELECT DISTINCT LTRIM(RTRIM(A8)) FROM #YHJ_TEMPFFGG73 WHERE NOT EXISTS
(SELECT 1 FROM #xtm124 WHERE LTRIM(RTRIM(A8))=LTRIM(RTRIM(wpsxmc)) AND wplbdm='07')
#2
数据也不少,我用版主的,也能返回值,但是放到真实数据里,没有返回值,怪事。
#3
把表数据导入到一张新表上试试,可能和索引有关系
#4
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB)
加一个 is not null,这个是典型的null问题。
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB where A1 is not null)
加一个 is not null,这个是典型的null问题。
SELECT * FROM YHJA WHERE A1 NOT IN (SELECT A1 FROM YHJB where A1 is not null)
#5
依sinat_28984567建议:
-- 重新建立表
SELECT LTRIM(RTRIM(wpsxmc)) wpsxmc
INTO YHJTABLEAA
FROM xtm124
WHERE wplbdm = '07'
--重建索引
ALTER INDEX ALL ON YHJTABLEAA REBUILD;
ALTER INDEX ALL ON YHJ_TEMPFFGG73 REBUILD;
ALTER INDEX ALL ON xtm124 REBUILD;
SELECT *
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT wpsxmc
FROM YHJTABLEAA
)
SELECT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' )
结果还是不理想,我想是不是排序规则的问题?
YHJ_TEMPFFGG73 Chinese_PRC_CS_AS_WS
xtm124 Chinese_PRC_CS_AS_WS
结果二者一样,奇怪了。
-- 重新建立表
SELECT LTRIM(RTRIM(wpsxmc)) wpsxmc
INTO YHJTABLEAA
FROM xtm124
WHERE wplbdm = '07'
--重建索引
ALTER INDEX ALL ON YHJTABLEAA REBUILD;
ALTER INDEX ALL ON YHJ_TEMPFFGG73 REBUILD;
ALTER INDEX ALL ON xtm124 REBUILD;
SELECT *
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT wpsxmc
FROM YHJTABLEAA
)
SELECT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' )
结果还是不理想,我想是不是排序规则的问题?
YHJ_TEMPFFGG73 Chinese_PRC_CS_AS_WS
xtm124 Chinese_PRC_CS_AS_WS
结果二者一样,奇怪了。
#6
确实像 wmxcn2000所说的NULL 问题:
SELECT DISTINCT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT DISTINCT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' AND wpsxmc IS NOT NULL )
-- 增加了 AND wpsxmc IS NOT NULL 就有结果了,又学习一招,非常感谢。
SELECT DISTINCT A8
FROM YHJ_TEMPFFGG73
WHERE LTRIM(RTRIM(A8)) NOT IN ( SELECT DISTINCT LTRIM(RTRIM(wpsxmc))
FROM xtm124
WHERE wplbdm = '07' AND wpsxmc IS NOT NULL )
-- 增加了 AND wpsxmc IS NOT NULL 就有结果了,又学习一招,非常感谢。