MYSQL - 获取用户和最新购买数据

时间:2017-04-04 06:39:59

标签: php mysql sql database

我正在尝试进行MYSQL查询,我最近为用户购买,然后查看是否符合特定条件。这是我放在一起的查询:

select
    users_purch.purch_date as purchase_date,
    users_purch.total_amount as purchase_amount,
    users.* 
from
    users 
left join
    (
        select
            max(date) as purch_date,
            user_id,
            total_amount 
        from
            users_purchases 
        group by
            user_id
    ) as users_purch 
        on users_purch.user_id = users.id 
where
    users_purch.purch_date < '2016-11-01' 
    and users_purch.total_cost < 112.49 
order by
    users_purch.purch_date desc

似乎查询有效但在某些方面失败。例如,如果用户有多个购买条目,则它将获得最大日期,但查询检索的total_cost金额不是与最大日期相同的行。如何重写此查询以完整地提供最新的购买记录?

谢谢!

3 个答案:

答案 0 :(得分:1)

您必须再次加入user_purchases表格才能获得有关日期的信息:

select
    users_purch.purch_date as purchase_date,
    users_purch.total_amount as purchase_amount,
    users.* 
from
    users 
left join
    (
        select
            max(date) as purch_date,
            user_id
        from
            users_purchases 
        group by
            user_id
    ) as users_purch 
        on users_purch.user_id = users.id 
left join
   (
       select
          user_id,
          date,
          total_amount
       from
          users_purchases
   ) as users_purch2 on users_purch.user_id = users_purch2.user_id and
        users_purch2.date = users_purch.purch_date
where
    users_purch.purch_date < '2016-11-01' 
    and users_purch.total_cost < 112.49 
order by
    users_purch.purch_date desc

答案 1 :(得分:0)

由于您在此选择

中有该组

如果你需要最大(日期)重写量,可以扩展适当的金额

  select
      users_purch.purch_date as purchase_date,
      users_purch.total_amount as purchase_amount,
      users.* 
  from
      users 
  left join
      (
        select t1.purch_date, t1.user_id, t2.total_amount from (
         select
                max(date) as purch_date,
                user_id
          from  users_purchases 
            group by  user_id ) t1
            inner join (
              date,
              user_id,
              total_amount 
          from users_purchases 
              ) t2 on t1.user_id= t2.user_id, t1.purch_date = t2.date

      ) as users_purch 
          on users_purch.user_id = users.id 
  where
      users_purch.purch_date < '2016-11-01' 
      and users_purch.total_cost < 112.49 
  order by
      users_purch.purch_date desc

答案 2 :(得分:0)

你可以尝试这个,伙计:

SELECT
    up.date AS 'purchase_date', 
    up.total_amount AS 'purchase_amount',
    u.*
FROM 
    users u
    INNER JOIN users_purchases up ON up.user_id = u.user_id
    INNER JOIN (
        # get max date per user_id
        SELECT user_id, max(date) AS 'purch_date'
        FROM users_purchases 
        GROUP BY user_id
    ) max_up ON 
        max_up.user_id
        # join that date to get the correct total_amount
        AND max_up.`purch_date` = up.`date`
WHERE 
    up.`date` < '2016-11-01'
    AND up.total_amount < 112.49
GROUP BY u.user_id
ORDER BY up.`date` DESC;

注意

  • user_id
  • 的最长日期
  • 每个最高日期total_amount的值
  • user_id
  • 分组
  • 使用users_purchases订购降序。date