mysql - 如何在同一个表上加入两个选择

标签 mysql

我有两个选择语句

Select author_id, count(text) from posts group by author_id

select author_id, count(text) from posts where postcounter =1 group by author_id

有没有办法在一个查询中合并这两个语句?结果的长度不同,因此需要在第二个结果集中插入一些0。

非常感谢您的帮助

最好的问候, 西蒙妮

最佳答案

您应该能够在单个查询中使用:

Select author_id, 
  count(text) TextCount,
  count(case when postcounter=1 then text end) PostCount
from posts 
group by author_id

关于mysql - 如何在同一个表上加入两个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904398/

相关文章:

MySQL 如何从两个表中选择数据,以便第二个表中的行覆盖第一个表中的行?

mysql - 无法创建表 #1113 - 表必须至少有 1 列

php - (SQL) 更新包含空外键错误的行

php - 无重复日期

MySQL 根据库存水平搜索受影响的行

MySQL 查询失败

mysql - 如何找到类别的利润

mysql - 使用带有聚合字段的 Order by 和 Group By 函数对 SQL 查询结果进行排序时遇到问题

mysql - 如何选择一列日期并使用它来驱动第二个 SELECT 语句?

php - Sql 连接查询错误。有人请帮助我