Mysql查询得到单个命令

时间:2016-05-02 10:01:39

标签: php mysql

大家好,请帮帮我。

我希望通过单笔订单获得客户。

我有客户表。

在我的客户表中我有

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

我想通过单笔订单显示客户。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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,应该解决目的。