在SQL中查找不匹配的数据

时间:2016-04-23 06:26:52

标签: sql sql-server

我有这两个简单的表格 enter image description here

我想通过比较SAMPLE2中的FruitName来从SAMPLE1中选择不匹配的数据 到目前为止我已经尝试了

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

但这给了我7条记录 enter image description here 我想要的输出是什么 enter image description here

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

试试这个:

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

必须确保您正在比较相同的ID。

答案 2 :(得分:1)

也许有帮助:

username = input("Enter your username: ")
password = input("Enter your password: ")

#hashing
p = len(password)+3
hashValue = 0
for element in password:
    hashValue += ord(element)*(37**(p*3+2))
    p += 1

#file reading
searchfile = open("users.txt","r")
for line in searchfile:
    if username in line:
        print(line)
        passwordFile = next(searchfile)
        if hashValue == int(passwordFile):
            print("Succesfully logged in.")
        else:
            print("Denied.")

SQLFiddle