使用代码点火器计算并从多个表中获取最佳记录

时间:2018-04-24 16:16:53

标签: php codeigniter

我有两个MySQL表

parent and child
pid pitem
1   a
2   b
3   c

child
cid citem pid
1    aa    1
2    bb    1
3    cc    1
4    dd    2
5    ff    2
6    gg    3

我想要以下结果

pid pitem count records of child table
1    a          3
2    b          2
3    c          1

如何使用join或sub查询?

1 个答案:

答案 0 :(得分:0)

您需要按照此代码使用group加入table和count。

<i>SELECT parent.pid, parent.pitem,count(pitem) as totalitem FROM parent  inner join child on parent.pid = child.pid group by parent.pid</i>