0

我想知道如何比较两条信息,例如我的查询中有两列:surnameforename. 我如何检查列中是否forename存在,surname反之亦然?

到目前为止,我已经创建了两个临时表,一个只选择surname,另一个选择所有其他信息。我打算通过加入来比较它们,但我没有运气。

4

2 回答 2

0

就像是:

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
于 2012-11-07T12:03:48.363 回答
0

在不知道表结构的情况下,编写查询有点困难。但我会做这样的事情;根据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
于 2012-11-07T12:41:07.023 回答