【问题标题】:How can I apply mutliple filters on my sql tables如何在 mysql 表上应用多个过滤器
【发布时间】:2019-07-21 09:55:14
【问题描述】:

我正在为 sql 中的关系划分而苦苦挣扎。我需要根据多个条件从同一个表中过滤数据。下面是我的表的架构。

| -------|----------------|-----------------|----------------|
|ID      |Question        |RespondentId     |Answer          |
| -------|----------------|-----------------|----------------|
|1       |Big             |1                |Yes             |
|2       |Big             |2                |Yes             |
|3       |Big             |3                |No              |
|4       |Gender          |1                |Male            |
|5       |Gender          |2                |Female          |
|6       |Gender          |3                |Female          |
|7       |Children        |1                |No              |
|8       |Children        |2                |Yes             |
|9       |Children        |3                |No              |
--------------------------------------------------------------

我需要这个名为 Answers 的表中的 RespondenId,它们与以下过滤器匹配:Question = Big ChildrenAnswer = YesYes 分别针对每个问题。因此,如果我有一个正确的 Sql 查询,我的结果应该返回以下数组:[2] 因为对于问题 ,唯一有答案的行 Yes >BigYes 问题 Children 的答案是 RespondentId = 2。

此外,提供的问题和答案不是固定的,应该是模块化的。例如,我应该能够更改答案或删除问题,而无需更改查询的整个结构。

你能帮我找到一个正确的查询这个问题吗?我一直在寻找@Erwin Brandstetter 提供的很多解释,但没有一个符合我的需求。

【问题讨论】:

    标签: sql database postgresql filter relational-division


    【解决方案1】:

    我会这样做:

    select a.RespondentId 
    from Answers a
    when (question, answer) in ( ('Big', 'Yes'), ('Children', 'Yes') )
    group by RespondentId
    having count(*) = 2 ;
    

    这很容易概括为:

    with qa as (
          select v.*
          from (values ('Big', 'Yes'), ('Children', 'Yes')
         ) v(question, answer)
    select a.RespondentId 
    from Answers a join
         qa
         on a.question = qa.question and a.answer = qa.answer
    group by RespondentId
    having count(*) = (select count(*) from qa);
    

    这是相当普遍的。您甚至可以安排 CTE 采用数组或 json 参数并解析为单独的比较值。

    【讨论】:

      【解决方案2】:

      您可以检查与问题匹配的行的计数 = 2 的结果并回答 where 条件

      select RespondentId 
      from Answers 
      when question in ( 'Big', 'Children')
      and Answer ='Yes' 
      group by RespondentId having count(*) = 2 
      

      【讨论】:

      • 非常感谢@scaisEdge 的回答,但在这种情况下,如果我希望我对儿童问题的回答是“不”,我会被这种查询结构所困扰..
      • @MartinDelobbe 。我的回答是对还是错.. ??
      • 它回答了我问题的第一部分,但没有回答关于模块化的第二部分
      • 一个查询就是一个查询..并根据条件过滤..你不能有一个查询来管理所有的条件....这是SQL的思维方式......您应该深入了解用于正确管理 SQL 的集合理论
      • 这是肯定的,但你的答案对我的用例来说太具体了。但是,如果您有其他解决方案,我会非常高兴!
      【解决方案3】:

      我认为您正在寻找的是 Pivot 表格。不同的数据库有不同的语法。您有效地将“问题”列的值转换为它们自己的列,然后查找符合您的条件的行。

      这是标准 SQL 中的一个低效示例,我为每个问题创建一个表,并使用 RespondentId 将它们连接到一个表中。

      select respondent_id from (select * from answers where question = 'Big') as big join (select * from answers where question = 'Children') as children on big.respondent_id = children.respondent_id where big.answer = 'Yes' and children.answer = 'Yes';

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        相关资源
        最近更新 更多