gpt4 book ai didi

mysql - SQL 选择名字但不包含姓氏匹配

转载 作者:行者123 更新时间:2023-11-29 21:21:12 25 4
gpt4 key购买 nike

我有一个包含名字和姓氏的表格。

FirstName   LastName
John Smith
John Taylor
Steve White
Adam Scott
Jane Smith
Jane Brown

我想选择不包含“Smith”的姓氏。如果匹配,请勿使用任何相同的名字

输出结果

FirstName   LastName
Steve White
Adam Scott

请注意,“John Taylor”和“Jane Brown”不在结果中,因为另一个 John 和 Jane 名字包含 Smith。

我当前的查询(包括约翰·泰勒和简·布朗):

Select FirstName, LastName
From tablPerson
where LastName != "Smith"

最佳答案

您可以使用不存在来做到这一点:

select p.*
from tablPerson p
where not exists (select 1
from tablPerson p2
where p2.LastName = 'Smith' and p2.FirstName = p.FirstName
);

对于这种类型的查询,我更喜欢不存在,因为如果任何名字为NULL,它的行为会更直观。如果'Smith' 的任何一个名字为NULL,则not in 返回空集。

关于mysql - SQL 选择名字但不包含姓氏匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com