我想知道如何比较两条信息,例如我的查询中有两列:surname
和forename
. 我如何检查列中是否forename
存在,surname
反之亦然?
到目前为止,我已经创建了两个临时表,一个只选择surname
,另一个选择所有其他信息。我打算通过加入来比较它们,但我没有运气。
就像是:
Select u1.Surname, u1.Forename,
(select count(0) from users u2 where u1.surname = u2.forename) as CountWhereForenameEqualsMySurname,
(select count(0) from users u2 where u1.forename = u2.surname ) as CountWhereSurnameEqualsMyForename
From users u1
在不知道表结构的情况下,编写查询有点困难。但我会做这样的事情;根据surname
等于. 将表连接到自身forename
。
SELECT
t1.forename as foreName1,
t1.surname as surName1,
t2.forename as foreName2,
t2.surname as surName2
FROM
tableName as t1 INNER JOIN tablename as t2
ON
t1.surname = t2.forename