【问题标题】:How do I run a select query with count()?如何使用 count() 运行选择查询?
【发布时间】:2012-06-30 05:47:14
【问题描述】:
account_users
-----
id | primary_key

posts
-----
post_id | primary_key
author | foreign key to account_users.id

假设我有 2 张桌子。 account_users 有用户。帖子保存帖子。

如何选择所有帖子数量(*)超过 5 个的用户?

【问题讨论】:

    标签: mysql database indexing


    【解决方案1】:

    试试这个

    SELECT au.*,p.*
    FROM account_users au
    INNER JOIN posts p
    ON p.account_users.id = au.id
    GROUP BY p.post_id
    HAVING count(*) > 5
    

    【讨论】:

      【解决方案2】:

      @Timex

      试试下面的查询。

      Select au.ID from account_users au
      inner join posts p on au.ID = p.author
      Group By au.ID
      having COUNT(p.post_id) > 5
      

      【讨论】:

        猜你喜欢
        • 2012-04-13
        • 2010-10-09
        • 1970-01-01
        • 2017-06-18
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        • 2012-11-30
        • 1970-01-01
        相关资源
        最近更新 更多