Mysql查询数据子集

标签 mysql prestashop

我有 2 个查询,第一个查询是列出从一开始就进行购买的所有客户:

SELECT o.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, c.email, COUNT(o.id_customer) AS 'total'
FROM ps_orders AS o
LEFT JOIN ps_order_state_lang AS osl ON o.current_state = osl.id_order_state
LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer
WHERE o.current_state IN(2,4,5)
AND osl.id_lang = 2
AND o.invoice_date BETWEEN '2014-01-01' AND '2015-09-30'
GROUP BY o.id_customer
ORDER BY total DESC;

虽然第二个查询是列出在特定时间段进行购买的所有客户:

SELECT c.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, COUNT(c.id_customer) AS total
FROM ps_orders AS o
LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer
WHERE o.invoice_date BETWEEN '2015-01-01' AND '2015-01-31'
AND o.current_state IN (2,4,5)
GROUP BY c.id_customer
ORDER BY total DESC;

基于此,我如何根据第二个查询中指定的时间段获取所有只进行了第一次购买的客户。我如何使用这两个查询来做到这一点?谢谢。

编辑#1

我目前的解决方案是这样的:

SELECT a.id_customer, a.fullname, a.total AS 'this_month', b.total AS 'all_time'
FROM (
    SELECT c.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, COUNT(c.id_customer) AS total
    FROM ps_orders AS o
    LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer
    WHERE o.invoice_date BETWEEN '2015-01-01' AND '2015-01-31'
    AND o.current_state IN (2,4,5)
    GROUP BY c.id_customer
    HAVING COUNT(c.id_customer) < 2
    ORDER BY total DESC
) AS a
LEFT JOIN (
    SELECT o.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, c.email, COUNT(o.id_customer) AS 'total'
    FROM ps_orders AS o
    LEFT JOIN ps_order_state_lang AS osl ON o.current_state = osl.id_order_state
    LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer
    WHERE o.current_state IN(2,4,5)
    AND osl.id_lang = 2
    AND o.invoice_date BETWEEN '2013-12-31' AND '2015-01-01'
    GROUP BY o.id_customer
    HAVING COUNT(o.id_customer) < 2
    ORDER BY total DESC
) AS b ON b.id_customer = a.id_customer
ORDER BY all_time DESC;

有更好的方法吗?

编辑 2:

第一个查询是从一开始就在我们商店购买过的所有客户的列表。

第二个查询是只在特定月份进行购买的所有客户的列表。

基于这 2 个查询,我想列出在特定月份首次购买的所有客户。

编辑#3: 也许我对我想要实现的目标不是很清楚,所以让我详细说明一下。

我有一个名为ps_orders 的表,它存储了从一开始的所有交易记录。来自 ps_orders 我对以下列特别感兴趣:

  • id_customer
  • id_order
  • 发票日期

我想知道在给定时间段内有多少首次购买者。例如:

我想知道有多少客户是 2015 年 1 月 1 日至 2015 年 1 月 31 日之间的首次购买者。以下查询列出了在该确切日期之间下达的所有订单:

SELECT c.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, COUNT(c.id_customer) AS total
FROM ps_orders AS o
LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer
WHERE o.invoice_date BETWEEN '2015-01-01' AND '2015-01-31'
AND o.current_state IN (2,4,5)
GROUP BY c.id_customer
ORDER BY total DESC;

结果如何包括较早购买的客户(例如,客户 ID 1234 可能在前一年购买过)。然而,我只对那段时间内的第一次购买者感兴趣。请参阅编辑 #1 以查看我当前的解决方案(它包含一些无效数据,但我认为我正在接近我想要的)

最佳答案

有点像

SELECT o.id_customer, MIN(o.invoice_date) first_purchase
FROM ps_orders AS o
GROUP BY o.id_customer
HAVING first_purchase BETWEEN '2015-01-01' AND '2015-01-31'

应该可以,不是吗?如果您只想要用户首次购买的日期,我不明白历史购买有何相关性。

因为您似乎还需要客户名称和电子邮件,所以使用这个初始查询为您自己生成一个连接表

SELECT c.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS fullname, c.email, firsts.first_purchase
FROM (
    SELECT
        o.id_customer cid,
        MIN(o.invoice_date) first_purchase
    FROM ps_orders AS o
    GROUP BY o.id_customer
    HAVING first_purchase BETWEEN '2015-01-01' AND '2015-01-31'
) firsts
LEFT JOIN customer c ON firsts.cid = c.id_customer
# If you prefer you may replace the HAVING above with a WHERE here
# WHERE first_purchase BETWEEN '2015-01-01' AND '2015-01-31'

关于Mysql查询数据子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33165347/

相关文章:

mysql - Magento 从数据库转储中导出产品、类别

mysql - Mysql Innodb性能-如何最小化多列索引?

php - hookActionProductUpdate 在模块中调用两次

css - 如何在 Prestashop 1.6 的运输页面中删除不需要承运人的标签以及如何在管理左侧菜单中添加垂直滚动

mysql - Prestashop 导出从 1.5 到 1.7

JQuery 显示属性不起作用 - prestashop

php - 如何检查用户是否在 Prestashop 的结帐页面

mysql - 如何重启mysql服务器?

mysql - 插入 MySQL 表或更新(如果存在)

mysql - 将三个查询的结果放到一张表中