【发布时间】: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 个查询的结果的结果。请帮帮我!
谢谢!
【问题讨论】:
如何在 mysql 中组合这两个查询?
select count(*) as entry_count from tbl_entries where user_id = x
和
select username, avatar from tbl_users where user_id = x
我想要一个结合这 2 个查询的结果的结果。请帮帮我!
谢谢!
【问题讨论】:
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) as entry_count
from tbl_users
where user_id = x
【讨论】:
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) AS cnt
from tbl_users
where user_id = x
【讨论】:
试试这个:
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
【讨论】:
试试这个:
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;
【讨论】:
选择用户名, 头像, (select count(*) from tbl_entries where user_id = U.user_id) AS cnt FROM tbl_users AS U WHERE user_id = x
【讨论】: