【问题标题】:sql rows containing column twice (or more)包含两次(或更多)列的 sql 行
【发布时间】:2014-12-18 20:57:58
【问题描述】:

使用 PhpMyAdmin,我正在尝试检索具有 所有 一组给定设施的房间列表。我可以查询包含所需设施的房间表,但我不知道如何过滤掉每个设施出现的房间。

这是我的查询:

SELECT distinct * FROM `Facilities` WHERE `Facility` = 'DVD Player' UNION
SELECT distinct * from `Facilities` WHERE `Facility` = 'Computer';

当我使用Group By 'Room' Having Count(Distinct 'Room') = 2 时,我得到了正确的表,但如果我选择Computer 然后合并它并选择DVD Player,我不会得到正确的表。

我在任何分组之前的初始查询显示:

Room    | Facility
A.0.0.1   Computer
G.0.02    Computer
HH.0.23   Computer
G.0.02    DVD Player
HH.0.23   DVD Player

我想要有电脑和 DVD 播放器的房间。我的表中有 22 个设施,我可能希望能够查询它们的任意组合以找到匹配的房间。

【问题讨论】:

    标签: mysql sql database phpmyadmin


    【解决方案1】:

    如果您想要配备特定设施的房间,请尝试以下方法:

    select f.room
    from facilities f
    where f.facility in ('Computer', 'DVD Player')
    group by f.room
    having count(distinct f.facility) = 2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2021-07-07
      • 2019-03-09
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多