大家好,请帮帮我。
我想通过单一订单获得客户。
我有客户桌。
在我的客户表中,我有
cu_id, cu_fname and cu_lname, cu_datecreated
我有订单表。
在我的订单表中,我有
o_id, cu_id, cu_dateorder
这是的桌子
客户
cu_id|cu_fname|cu_lname|cu_datecreated
1 |joe |qwe |March-01-2016
2 |asd |gfh |March-03-2016
3 |zxc |vbn |March-05-2016
订单
o_id|cu_id|cu_dateorder
1 |1 |03-05-2016
2 |2 |03-10-2016
3 |2 |03-13-2016
4 |3 |03-20-2016
5 |1 |04-23-2016
我想显示具有单个订单的客户
select c.cu_id,c.cu_fname,c.cu_lname,c.cu_datacreated,o.o_id,o.cu_dateorder
from customer c
inner join order o on c.cu_id = o.cu_id
group by o.cu_id
having count(o.o_id)=1
select a.cu_id,a.cu_fname from customer a join order b on a.cu_id = b.cu_id
group by b.cu_id having count(cu_id) = 1;
结果集将按客户id分组,并且仅在以下位置显示结果cu_id的计数是1,应该可以解决这个目的。