简体   繁体   中英

MYSQL - Get User and Most Recent Purchase Data

I am trying to do a MYSQL query where I get the most recent purchase for a user and then see if falls within certain criteria. Here is the query I put together:

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

It seems that the query works but fails in certain aspects. For example, if a user has more than one purchase entry it is getting the max date but the amount as total_cost that the query retrieves is not from the same row as the max date. How can I rewrite this query to give me the most recent purchase record in its entirety?

Thanks!

You have to join once more to user_purchases table in order to get the information about the date:

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

due the group by you have in this select

if you need the amount of the max(date) rewrite extecting the proper amount

  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

You can try this one, mate:

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;

Note

  • Max Date per user_id
  • Value of total_amount per Max Date
  • Grouped per user_id
  • Ordered descending using users_purchases . date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM