<bdo id='1hW7h'></bdo><ul id='1hW7h'></ul>

<small id='1hW7h'></small><noframes id='1hW7h'>

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

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

      来自多个表的MYSQL左连接计数

      时间:2023-06-26

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

        <bdo id='pGJTr'></bdo><ul id='pGJTr'></ul>

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

                <tbody id='pGJTr'></tbody>
              <tfoot id='pGJTr'></tfoot>
                本文介绍了来自多个表的MYSQL左连接计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想添加表示来自其他表的计数的列.

                I want to add columns that represent counts from other tables.

                我有 3 张桌子.

                消息

                MessageID    User      Message      Topic
                1            Tom       Hi           ball
                2            John      Hey          book
                3            Mike      Sup          book
                4            Mike      Ok           book
                

                主题

                Topic      Title     Category1    Category2
                ball       Sports    Action       Hot
                book       School    Study        Hot
                

                Stars_Given

                starID     Topic
                1          ball
                2          book
                3          book
                4          book
                

                我想结束:

                Topic_Review

                Topic    Title     StarCount    UserCount    MessageCount
                ball     Sports    1            1            1
                book     school    3            2            3
                

                所以基本上我想附加 3 列,其中包含唯一值的计数(每个主题中给出的星数、在主题中拥有消息的唯一用户以及每个主题中唯一消息的数量).

                So basically I want to attach 3 columns with counts of unique values (number of stars given within each topic, unique users who have messages within topic, and the number of unique messages in each topic).

                我希望最终也能够过滤类别(查看两列).

                I want to eventually be able to filter on the categories (look in both columns) as well.

                此外,我想最终按我加入的计数进行排序.例如,我将有一个按钮,按升序按星数"排序,或按降序按用户数"排序,等等.

                Also, I want to eventually sort by the counts that I join. Example, I'm going to have a button that sorts by "number of stars" by ascending order, or sort by "number of users" by descending order, etc.

                我尝试调整其他人的答案,但无法正常工作.

                I've tried adapting other people's answers and I can't get it to work properly.

                推荐答案

                select
                  t.Topic,
                  t.Title,
                  count(distinct s.starID) as StarCount,
                  count(distinct m.User) as UserCount,
                  count(distinct m.messageID) as MessageCount
                from
                  Topics t
                  left join Messages m ON m.Topic = t.Topic
                  left join Stars_Given s ON s.Topic = t.Topic
                group by
                  t.Topic,
                  t.Title
                

                Sql Fiddle

                或者,您可以在子查询中执行聚合,如果表中有大量数据,这可能会更有效:

                Sql Fiddle

                Or, you can perform the aggregation in sub-queries, which will likely be more efficient if you have a substantial amount of data in the tables:

                select
                  t.Topic,
                  t.Title,
                  s.StarCount,
                  m.UserCount,
                  m.MessageCount
                from
                  Topics t
                  left join (
                    select 
                      Topic, 
                      count(distinct User) as UserCount,
                      count(*) as MessageCount
                    from Messages
                    group by Topic
                  ) m ON m.Topic = t.Topic
                  left join (
                    select
                      Topic, 
                      count(*) as StarCount
                    from Stars_Given 
                    group by Topic
                  ) s ON s.Topic = t.Topic
                

                这篇关于来自多个表的MYSQL左连接计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:“SELECT COUNT(*)"很慢,即使有 where 子句 下一篇:MySQL 性能:多表与单表和分区上的索引

                相关文章

                <tfoot id='glFLc'></tfoot>

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

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

                    <legend id='glFLc'><style id='glFLc'><dir id='glFLc'><q id='glFLc'></q></dir></style></legend>

                      <bdo id='glFLc'></bdo><ul id='glFLc'></ul>