2012-02-14 111 views
3

如何將這兩個查詢結合在mysql中?mysql select count另一個選擇計數

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

select username, avatar from tbl_users where user_id = x 

我想要一個結果,結合這2個查詢的結果。請幫助我們!

謝謝!

回答

6
select username, 
     avatar, 
     (select count(*) from tbl_entries where user_id = x) as entry_count 
from tbl_users 
where user_id = x 
+0

太棒了!多謝,夥計!很好的幫助! – janusbalatbat 2012-02-14 08:25:11

1
select username, 
     avatar, 
     (select count(*) from tbl_entries where user_id = x) AS cnt 
    from tbl_users 
where user_id = x 
0

試試這個:

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 
0

試試這個:

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; 
0

SELECT用戶名, 頭像, (SELECT COUNT(*)從tbl_entries其中USER_ID = U .user_id)AS cnt FROM tbl_users AS U WHERE user_id = x