2011-06-10 40 views
0

我有這個查詢,如果表中沒有相同ID的金額等於或高於此值,應該插入一個金額。INSERT和WHERE不存在的怪異SQL行爲

我嘗試這樣做的MySQL 5.1和MSSQL 2K5下:

的MySQL

INSERT IGNORE INTO測試(ID,金額) SELECT 6,50 FROM測試 WHERE NOT EXISTS(SELECT 1 FROM測試WHERE量> = 50和ID = 6)LIMIT 1

MSSQL

INSERT INTO噸EST(ID,金額) SELECT 6,50 FROM測試 WHERE NOT EXISTS(SELECT TOP 11 FROM測試WHERE量> = 50和ID = 6)

該查詢工作正常,如果已經存在至少一個條目在桌子裏。如果表格是空的,它將無法工作。這與MySQL(5.1)和MSSQL(2005)下的行爲是一樣的。 我不明白爲什麼。任何人都有解釋和方法來解決這個查詢工作,即使表是完全空的?

編輯:我需要這個MySQL的主要...

更新:我開始了新的問題,特別爲MySQL: MySQL Problem with inserting a row with certain conditions

+0

是SELECT TOP 1 1 = SELECT TOP 1 *? – Chris 2011-06-10 16:02:44

+0

@Chris我認爲是這樣,並在一個「存在(選擇...」我認爲沒有頂部需要和1也不需要太 – Ice 2011-06-10 16:33:16

+0

我仍然無法使其在MySQL下工作...如果你有另一種方法可以做到這一點,這也可以..也許左連接可以工作嗎? – tink01 2011-06-10 16:35:56

回答

2

它失敗,因爲select語句根據返回的行數選擇6和50的值。由於你的表是空的 - 沒有行返回這些值。

其修改爲

INSERT INTO test (id, amount) SELECT 6, 50 WHERE NOT EXISTS (SELECT TOP 1 1 FROM test WHERE amount >= 50 AND id = 6) 
+0

它在MySQL中不起作用......它不喜歡在那裏沒有FROM子句:( – tink01 2011-06-10 16:27:29

+0

工作好,解決方案比我的小 – Ice 2011-06-10 16:29:26

+0

它在MySQL下不起作用,你不能有一個丟失的FROM子句,如果你添加它,當沒有行時,它仍然是同樣的問題... – tink01 2011-06-10 17:01:55

0

更改您的查詢,如下所示:

INSERT INTO test (id, amount) 
SELECT 6, 50 FROM test 
WHERE (SELECT count(*) FROM test WHERE amount >= 50 AND id = 6) = 0 
+0

當表爲空時影響0行:|當表中至少有一行時工作...奇怪的胡:P – tink01 2011-06-10 16:08:54

+0

這不工作像預期的那樣,我測試了並得到了其他解決方案 – Ice 2011-06-10 16:31:27

0

我想一步一步來:

-- create a test-table with one entry 
SELECT id = 1, amount = 50 
into #TEST 

-- see if statement works when table is not empty 
INSERT INTO #test (id, amount) 
SELECT id = 6, amount = 50 
FROM #test 
WHERE NOT EXISTS (SELECT * FROM #test WHERE amount >= 50 AND id = 6) 

-- show content of test-table 
select * from #test 

-- empty the test table 
truncate table #test 

-- try to insert a first dataentry 
INSERT INTO #test (id, amount) 
SELECT id = 6, amount = 50 
FROM #test 
WHERE NOT EXISTS (SELECT * FROM #test WHERE amount >= 50 AND id = 6) 
union 
SELECT id = 6, amount = 50 
where (SELECT C = COUNT(*) FROM #test WHERE amount >= 50 AND id = 6) = 0 

-- show content of test-table 
select * from #test 

-- remove Test-table 
drop table #test 

首先,我試圖重現行爲,是很奇怪的。工會 - 認爲管理空表的案件,並認爲這工作正常。

+0

同樣,它似乎並不適用於MySQL。 SELECT id = 6,amount = 50不起作用,MySQL似乎需要一個「FROM」子句。 :( – tink01 2011-06-10 16:31:14

0

好的,這是真正的問題。試試這個,使用第二個表(#測試2):

-- create a test-table with one entry 
SELECT id = 1, amount = 50 
into #TEST 

-- create a second test-table with one entry 
SELECT id = 1, amount = 50 
into #TEST2 

-- see if statement works when table is not empty 
INSERT INTO #test (id, amount) 
SELECT id = 6, amount = 50 
FROM #test 
WHERE NOT EXISTS (SELECT * FROM #test WHERE amount >= 50 AND id = 6) 

-- show content of test-table 
select * from #test 

-- empty the test table 
truncate table #test 

INSERT INTO #test (id, amount) SELECT 6, 50 WHERE NOT EXISTS (SELECT TOP 1 1 FROM #test WHERE amount >= 50 AND id = 6) 

-- try to insert a first dataentry 
INSERT INTO #test (id, amount) 
SELECT id = 6, amount = 50 
FROM #test 
WHERE NOT EXISTS (SELECT * FROM #test WHERE amount >= 50 AND id = 6) 
union 
SELECT id = 6, amount = 50 
from #test2 
where (SELECT C = COUNT(*) FROM #test) = 0 


-- show content of test-table 
select * from #test 

-- remove Test-table 
drop table #test 

(假設你知道與前述#表,這是在MS-SQL的臨時表)。

BonyT已經說明了原因。

+0

嘗試這樣MYSQL下: INSERT INTO測試(ID,金額) SELECT ID = 6,量= 50 FROM測試 WHERE NOT EXISTS(SELECT * FROM測試WHERE量> = 50和ID = 6) UNION SELECT id = 6,amount = 50 FROM test2 WHERE(SELECT COUNT(*)AS C FROM test)= 0 仍然是同樣的問題? – tink01 2011-06-10 17:00:42