在SQL

2016-04-23 73 views
0

找到無與倫比的數據我有這2個簡單的表格 enter image description here在SQL

我想在SAMPLE2 到目前爲止,我已經嘗試

SELECT * FROM SAMPLE1,SAMPLE2 WHERE SAMPLE1.FruitName NOT LIKE '%' + dbo.SAMPLE2.FruitName +'%' 

比較FruitName選擇從SAMPLE1不匹配的數據,但是這給了我共7條 enter image description here 我想輸出是 enter image description here

+0

也有相匹配的ID字段,否則會與所有其他每個記錄相匹配。 –

回答

2
SELECT * 
FROM SAMPLE1 s1 
WHERE NOT EXISTS (
     SELECT NULL 
     FROM SAMPLE2 s2 
     WHERE s1.FruitName LIKE '%' + s2.FruitName + '%' 
     ) 
+0

哦,非常感謝你我努力工作,但沒有運氣,但隨着你的查詢它像魅力一樣工作非常感謝。上帝祝福你 – user3110224

1

試試這個:

SELECT * FROM SAMPLE1,SAMPLE2 WHERE SAMPLE1.FruitName NOT LIKE '%' + dbo.SAMPLE2.FruitName +'%' AND SAMPLE1.id = SAMPLE2.id 

必須確保您比較相同的ID。

1

也許這幫助:

select SAMPLE1.* from SAMPLE1 
Left join SAMPLE2 ON SAMPLE1.fruitName LIKE concat('%', SAMPLE2.fruitName, '%') 
Where SAMPLE2.id is null 

SQLFiddle