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

    • <bdo id='L6IzW'></bdo><ul id='L6IzW'></ul>

  1. <legend id='L6IzW'><style id='L6IzW'><dir id='L6IzW'><q id='L6IzW'></q></dir></style></legend>

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

    1. <tfoot id='L6IzW'></tfoot>

      Mysql:如何选择具有特定值的组?

      Mysql: how to select groups having certain values?(Mysql:如何选择具有特定值的组?)

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

        <tbody id='iOlqi'></tbody>

        • <bdo id='iOlqi'></bdo><ul id='iOlqi'></ul>

              <tfoot id='iOlqi'></tfoot>
              <legend id='iOlqi'><style id='iOlqi'><dir id='iOlqi'><q id='iOlqi'></q></dir></style></legend>
              1. <i id='iOlqi'><tr id='iOlqi'><dt id='iOlqi'><q id='iOlqi'><span id='iOlqi'><b id='iOlqi'><form id='iOlqi'><ins id='iOlqi'></ins><ul id='iOlqi'></ul><sub id='iOlqi'></sub></form><legend id='iOlqi'></legend><bdo id='iOlqi'><pre id='iOlqi'><center id='iOlqi'></center></pre></bdo></b><th id='iOlqi'></th></span></q></dt></tr></i><div id='iOlqi'><tfoot id='iOlqi'></tfoot><dl id='iOlqi'><fieldset id='iOlqi'></fieldset></dl></div>
              2. 本文介绍了Mysql:如何选择具有特定值的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                说有这样的表:

                mysql> SELECT * FROM tags;
                +---------+--------+
                | post_id | tag_id |
                +---------+--------+
                |       1 |      2 |
                |       1 |      3 |
                |       1 |      1 |
                |       2 |      1 |
                |       2 |      2 |
                +---------+--------+
                5 rows in set (0.00 sec)
                

                字段名称一目了然.我想选择同时具有 1 个和 3 个 tag_idpost_ids,所以在这个例子中它只有 1.我想到了类似的东西SELECT post_id FROM tags GROUP BY post_id HAVING ... 之后我想列出这个组中存在的 tag_ids.我该怎么做?

                Field names are pretty self-explanatory. I want to select post_ids that have both 1 and 3 tag_ids, so in this example it's only 1. I thought of something like SELECT post_id FROM tags GROUP BY post_id HAVING ... After having I'd like to list tag_ids that are present in this group. How do I do that?

                推荐答案

                如果没有任何唯一约束,请尝试:

                If there aren't any unique constraints try:

                SELECT post_id 
                FROM tags 
                WHERE tag_id = 1 OR tag_id = 3 
                GROUP BY post_id 
                HAVING count(DISTINCT tag_id) = 2;
                

                或者使用这个 HAVING 子句,如果试图只检测两个 tag_id 值:

                Or use this HAVING clause, if trying to detect only two tag_id values:

                HAVING MIN(tag_id) <> MAX(tag_id)
                

                如果 post_id 和 tag_id 都有唯一的约束,这也应该有效:

                If post_id and tag_id both have an unique constraint, this should work too:

                SELECT post_id 
                FROM tags 
                WHERE tag_id = 1 OR tag_id = 3 
                GROUP BY post_id 
                HAVING count(*) = 2;
                

                这篇关于Mysql:如何选择具有特定值的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  <tbody id='5BbDB'></tbody>
                <legend id='5BbDB'><style id='5BbDB'><dir id='5BbDB'><q id='5BbDB'></q></dir></style></legend>

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

                  <small id='5BbDB'></small><noframes id='5BbDB'>

                      <tfoot id='5BbDB'></tfoot>
                        <bdo id='5BbDB'></bdo><ul id='5BbDB'></ul>