【发布时间】:2021-09-18 16:43:32
【问题描述】:
我正在尝试在 MS SQL 中进行查询,但无法解决。我有一张员工表:
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
1 John Green James Sue null 101011 1
2 Michael Sloan Barry Lilly null 101011 1
3 Sally Green Andrew Molly Jemi 101011 1
4 Barry Sloan Soul Paul Lilly 101011 0
5 James Green Ned White Sue 101011 0
我想要一个查询来选择孩子的父亲姓名和母亲姓名与姓名和妻子姓名匹配的行。对于示例表,我想返回父亲和母亲姓名与姓名和妻子姓名列匹配的行的结果。例如。 id=1,其中 John 的父亲姓名 James 和母亲姓名 Sue 与 id 5 匹配,返回 James 作为名字,Sue 作为妻子姓名。所以我的查询应该返回(这是我的预期结果)
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
5 James Green Ned White Sue 101011 0
4 Barry Sloan Soul Paul Lilly 101011 0
我尝试使用以下查询,但它只检查 James。如何更改我的查询以检查所有名称并返回预期结果。
select * FROM employees
where first_name like '%James%'
and wife_name like '%Sue%'
and pincode=101011;
这方面的任何提示都会非常有帮助。我是新加入,需要帮助编写自我加入以获得结果。
【问题讨论】:
-
请分享上述示例的创建表和插入数据脚本。
标签: sql sql-server tsql sql-server-2008 join