MySQL查询:在这个月获得新客户

时间:2014-03-03 07:53:36

标签: mysql sql

我在表格中有数百万条记录,其中包含以下列:

id, item_id, customer_id, date

我想查找特定月份的customer_id,该月份首次插入该月的表格中。

什么是最好和最简单的查询。

由于

3 个答案:

答案 0 :(得分:3)

select customer_id , min(date) as min_date
from theTable
group by customer_id 
having min_date>=<the desired date>

答案 1 :(得分:2)

尝试类似:

SELECT
date_format(date,"%M") month,group_concat(customer_id ORDER BY customer_id) customer_ids
FROM
<table_name>
GROUP BY month;

答案 2 :(得分:1)

这样的事情可能是:

select distinct(id) from customer
where month(date)='Your_month'
order by date