mysql 选择计数与另一个选择计数

Posted

技术标签:

【中文标题】mysql 选择计数与另一个选择计数【英文标题】:mysql select count with another select count 【发布时间】:2012-03-05 15:03:24 【问题描述】:

如何在 mysql 中组合这两个查询?

 select count(*) as entry_count from tbl_entries where user_id = x

select username, avatar from tbl_users where user_id = x

我想要一个结合这 2 个查询的结果的结果。请帮帮我!

谢谢!

【问题讨论】:

【参考方案1】:
select username, 
       avatar,
       (select count(*) from tbl_entries where user_id = x) as entry_count
from tbl_users 
where user_id = x

【讨论】:

【参考方案2】:
select username,
       avatar,
       (select count(*) from tbl_entries where user_id = x) AS cnt
  from tbl_users
 where user_id = x

【讨论】:

【参考方案3】:

试试这个:

SELECT a.username,
        a.avatar,
        COUNT(*) as entry_count,
FROM tbl_Users a LEFT JOIN tbl_entries b ON
        a.user_ID = b.user_ID
WHERE a.user_ID = x
GROUP BY a.username

【讨论】:

【参考方案4】:

试试这个:

SELECT U.username, U.avatar, 
    count(*) AS entry_count 
FROM tbl_users AS U
LEFT JOIN tbl_entries AS E ON U.user_id = E.user_id
WHERE user_id = x
GROUP BY U.user_id;

【讨论】:

【参考方案5】:

选择用户名, 头像, (select count(*) from tbl_entries where user_id = U.user_id) AS cnt FROM tbl_users AS U WHERE user_id = x

【讨论】:

以上是关于mysql 选择计数与另一个选择计数的主要内容,如果未能解决你的问题,请参考以下文章

MySQL选择不同的计数

MySQL按值选择计数

MySQL 选择计数与 EXISTS

Mysql如何以10分钟间隔选择计数聚合结果

选择计数(*)和选择计数(字段名)之间的区别[重复]

mysql选择当前位置的计数器[重复]