【问题标题】:SQL - Returning all rows that are not 'vice versa' of one anotherSQL - 返回不是“反之亦然”的所有行
【发布时间】:2021-09-05 17:56:46
【问题描述】:

我遇到了如下情况(请注意,此设置已创建为最小的可重现示例)。我有一张房子的桌子,是这样创建的:

CREATE TABLE houses (
    id character varying(24) NOT NULL,
);

我有一个名为 house_listings 的大表,如下所示:

CREATE TABLE house_listings(
    id character varying(24) NOT NULL,
    house_one character varying(24) NOT NULL,
    house_two character varying(24) NOT NULL,
    for_sale bit NOT NULL,
    PRIMARY KEY (house_one, house_two),
    FOREIGN KEY (house_one) REFERENCES houses(id),
    FOREIGN KEY (house_two) REFERENCES houses(id)
);

所以,假设我运行以下查询:

select * from house_listings where house_one = '12345';

我得到以下输出:

+------------------------+------------------------+-------------------+--------------+
| id                     | house_one              | house_two         | for_sale     |
+------------------------+------------------------+-------------------+--------------+
| 9RV3fyeJuUbAsHYdAb2Qnm | 12345                  | 8888              | 0x00         |
+------------------------+------------------------+-------------------+--------------+

然后我运行以下查询:

select * from house_listings where house_two = '12345' 我得到以下结果:

+------------------------+-------------------+------------------------+--------------+
| id                     | house_one         | house_two              | for_sale     |
+------------------------+-------------------+------------------------+--------------+
| 1oFH1oDkLyng6ZHNafqcXr | 8888              | 12345                  | 0x00         |
| vW4eNAC7jgZVxWAGxH4xAR | 7777              | 12345                  | 0x00         |
+------------------------+-------------------+------------------------+--------------+

注意此结果的第一行如何以一种反之亦然的方式对应于另一个结果的第一行(即在本例中,house_one 和 house_two 被翻转)。

然而,我在这里感兴趣的是这个结果的第二行。其中 house_one 是 7777,而 house_two 是 12345。

我想以某种方式运行一个查询,该查询基本上将返回所有行,如第二个。所以,给定一个特定的房子 id,比如 12345,它只会返回(在这个例子中):

+------------------------+-------------------+------------------------+--------------+
| id                     | house_one         | house_two              | for_sale     |
+------------------------+-------------------+------------------------+--------------+
| vW4eNAC7jgZVxWAGxH4xAR | 7777              | 12345                  | 0x00         |
+------------------------+-------------------+------------------------+--------------+

我一直在这里尝试自我加入,因为我认为这可能是最好的方法,但是加入 where house_one = house_two 并不是我想要的,我不确定如何最好地解决这个问题问题。

在此不胜感激!

【问题讨论】:

    标签: mysql sql join self-join


    【解决方案1】:

    你可以使用not exists:

    select hl.*
    from house_listings hl
    where not exists (select 1
                      from house_listings hl2
                      where hl2.house_one = hl1.house_two and
                            hl2.house_two = hl1.house_one
                     );
    

    这会返回所有这样的单例。当然,您可以在外部查询中添加where 子句,以限制为house_one 的特定值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多