我有一个正在处理SqlClient的select语句:
command.CommandText = "SELECT TOP 20 ID
FROM Tank
WHERE ID NOT IN
(SELECT TOP 10 " + "ID
FROM Tank
ORDER BY ID)
ORDER BY ID";
尝试在OLEDBCommand中使用相同的命令不起作用,并提供语法错误接近NOT IN。所以我认为我不存在,但这也不起作用..这是命令:
command.CommandText = "SELECT TOP 20 ID
FROM Tank AS Table1
WHERE NOT EXISTS (SELECT TOP 10 ID
FROM Tank AS Table2
WHERE Table1.ID = Table2.ID
ORDER BY ID)
ORDER BY ID";
该命令执行但总是返回空。我尝试使用exists来查看它是否有效,并且我确实返回了前10行。我需要最后10个,MS Access似乎不接受NOT IN。
任何建议?
编辑:
然后我创建的命令(命令)被添加到adapter.SelectCommand(adapter.SelectCommand = command),然后我执行一个adapter.Fill(Table),其中Table是一个DataTable。这在我使用以下命令(没有NOT)时起作用:
command.CommandText = "SELECT TOP 20 ID
FROM Tank
WHERE ID IN
(SELECT TOP 10 " + "ID
FROM Tank
ORDER BY ID)
ORDER BY ID";
但是,当我添加NOT时,Adapter.Fill(表)只会停滞并卡住。它永远不会离开。这是命令:
command.CommandText = "SELECT TOP 20 ID
FROM Tank
WHERE ID IN
(SELECT TOP 10 " + "ID
FROM Tank
ORDER BY ID)
ORDER BY ID";
但是当我添加NOT
时答案 0 :(得分:0)
table
和1
之间有空格。大概你的意思是:
SELECT TOP 20 ID
FROM Tank AS Table1
------------------^
WHERE NOT EXISTS (SELECT TOP 10 ID FROM Tank AS Table2 WHERE Table1.ID = Table2.ID ORDER BY ID)
ORDER BY ID
答案 1 :(得分:0)
子选择问题中的旧NULL?
如果子选择返回一个(或多个)NULL,则" NOT IN(子选择"将返回空结果集。
NOT IN有点危险。要么重写为NOT EXISTS,要么确保子选择永远不会返回任何NULL。 (例如,通过向子选择添加IS NOT NULL。)