我在数据库中有两个表,看起来像这样
成员表1
id | memberid | name | password
-------------------------------
Journal table2
id | memberid | title | desc
-----------------------------
我想从表1中选择所有成员,加入表2中每个成员的结果journal_count。我试图计算每个成员在表2中出现的次数
使用mysql和php,有人可以帮我解决这个问题吗
感谢
答案 0 :(得分:5)
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
group by m.memberid
P.S。如果你的表有一个名为desc的字段,请注意这是一个保留字,最好重命名它。否则你总是要把它放在反引号中。
select m.memberid,m.name,count(j.memberid) as total
from members as m
left join journal as j
on m.memberid = j.memberid
where m.memberid = $memberid
group by m.memberid
答案 1 :(得分:2)
select members.name, count(journal.memberid) as journal_count from members, journal where members.memberid = journal.memberid group by journal.memberid