• <bdo id='1gB4B'></bdo><ul id='1gB4B'></ul>
  • <legend id='1gB4B'><style id='1gB4B'><dir id='1gB4B'><q id='1gB4B'></q></dir></style></legend>

    1. <tfoot id='1gB4B'></tfoot>
      <i id='1gB4B'><tr id='1gB4B'><dt id='1gB4B'><q id='1gB4B'><span id='1gB4B'><b id='1gB4B'><form id='1gB4B'><ins id='1gB4B'></ins><ul id='1gB4B'></ul><sub id='1gB4B'></sub></form><legend id='1gB4B'></legend><bdo id='1gB4B'><pre id='1gB4B'><center id='1gB4B'></center></pre></bdo></b><th id='1gB4B'></th></span></q></dt></tr></i><div id='1gB4B'><tfoot id='1gB4B'></tfoot><dl id='1gB4B'><fieldset id='1gB4B'></fieldset></dl></div>

      1. <small id='1gB4B'></small><noframes id='1gB4B'>

        SQL 仅选择存在多个关系的行

        时间:2023-10-08

        <small id='zDIRu'></small><noframes id='zDIRu'>

        • <bdo id='zDIRu'></bdo><ul id='zDIRu'></ul>
          <i id='zDIRu'><tr id='zDIRu'><dt id='zDIRu'><q id='zDIRu'><span id='zDIRu'><b id='zDIRu'><form id='zDIRu'><ins id='zDIRu'></ins><ul id='zDIRu'></ul><sub id='zDIRu'></sub></form><legend id='zDIRu'></legend><bdo id='zDIRu'><pre id='zDIRu'><center id='zDIRu'></center></pre></bdo></b><th id='zDIRu'></th></span></q></dt></tr></i><div id='zDIRu'><tfoot id='zDIRu'></tfoot><dl id='zDIRu'><fieldset id='zDIRu'></fieldset></dl></div>

          <tfoot id='zDIRu'></tfoot>
          <legend id='zDIRu'><style id='zDIRu'><dir id='zDIRu'><q id='zDIRu'></q></dir></style></legend>

                <tbody id='zDIRu'></tbody>

                • 本文介绍了SQL 仅选择存在多个关系的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给定一个父表'父'

                  ╔═══════════╦══════════╗
                  ║ PARENT_ID ║   NAME   ║
                  ╠═══════════╬══════════╣
                  ║         1 ║ bob      ║
                  ║         2 ║ carol    ║
                  ║         3 ║ stew     ║
                  ╚═══════════╩══════════╝
                  

                  以及父级和(此处未指定)属性表之间的多对多关系表rel"

                  and a many-many relationship table 'rel' between parent and a (here unspecified) property table

                  ╔═══════════╦═══════════╗
                  ║ PARENT_ID ║  PROP_ID  ║
                  ╠═══════════╬═══════════╣
                  ║         1 ║         5 ║
                  ║         1 ║         1 ║
                  ║         2 ║         5 ║
                  ║         2 ║         4 ║
                  ║         2 ║         1 ║
                  ║         3 ║         1 ║
                  ║         3 ║         3 ║
                  ╚═══════════╩═══════════╝
                  

                  如何选择具有所有一组指定关系的所有父级?例如.使用示例数据,如何找到同时拥有属性 5 和属性 1 的所有父母?

                  How can I select all parents that have all of a specified set of relationships? E.g. with the sample data, how can I find all parents that have both property 5 and 1?

                  同样的问题,但要求 完全 匹配:SQL 仅选择存在精确多重关系的行

                  edit: Same question but with requirement for an exact match: SQL Select only rows where exact multiple relationships exist

                  推荐答案

                  这叫做 关系划分

                  This is called Relational Division

                  SELECT  a.name
                  FROM    parent a
                          INNER JOIN rel b
                              ON a.parent_ID = b.parent_ID
                  WHERE   b.prop_id IN (1,5)
                  GROUP BY a.name
                  HAVING COUNT(*) = 2
                  

                  • SQLFiddle 演示链接
                  • 更新 1

                    如果没有对每个 parent_idprop_id 强制实施 唯一约束,则在这种情况下需要 DISTINCT.

                    if unique constraint was not enforce on prop_id for every parent_id, DISTINCT is needed on this case.

                    SELECT  a.name
                    FROM    parent a
                            INNER JOIN rel b
                                ON a.parent_ID = b.parent_ID
                    WHERE   b.prop_id IN (1,5)
                    GROUP BY a.name
                    HAVING COUNT(DISTINCT b.prop_id) = 2
                    

                    这篇关于SQL 仅选择存在多个关系的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL选择带有日期的行 下一篇:在 SELECT 语句中提取分隔值右侧的字符

                  相关文章

                    <bdo id='13D0R'></bdo><ul id='13D0R'></ul>

                    <tfoot id='13D0R'></tfoot>

                    <i id='13D0R'><tr id='13D0R'><dt id='13D0R'><q id='13D0R'><span id='13D0R'><b id='13D0R'><form id='13D0R'><ins id='13D0R'></ins><ul id='13D0R'></ul><sub id='13D0R'></sub></form><legend id='13D0R'></legend><bdo id='13D0R'><pre id='13D0R'><center id='13D0R'></center></pre></bdo></b><th id='13D0R'></th></span></q></dt></tr></i><div id='13D0R'><tfoot id='13D0R'></tfoot><dl id='13D0R'><fieldset id='13D0R'></fieldset></dl></div>
                    <legend id='13D0R'><style id='13D0R'><dir id='13D0R'><q id='13D0R'></q></dir></style></legend>
                  1. <small id='13D0R'></small><noframes id='13D0R'>