我尝试列出第一个订单日期的所有客户记录
表客户
customer_id | customer_name
1 + John
2 + Brad
表格订单
order_id | customer_id | date
1 + 1 + 2013-10-12 01:00:00
2 + 1 + 2013-11-18 09:00:00
3 + 2 + 2013-11-21 02:00:00
的MySQL
$get_ch_raw = "select c.*, a.*, ch.* from `customers` c left join `admin` a on
a.admin_id=c.admin_id left join `call_history` ch on
ch.customer_id=c.customers_id
order by c.customers_id desc"
,结果应该是这样的:
1 + John + 2013-10-12 01:00:00
2 + Brad + 2013-11-21 02:00:00
在这种情况下,我不确定如何使用子查询选择第一个订单。
答案 0 :(得分:1)
这应该这样做:
SELECT c.customer_id, c.customer_name, MIN(o.date) date FROM customers c
LEFT JOIN orders o USING (customer_id)
GROUP BY c.customer_id
小提琴here。