简体   繁体   中英

MySQL SELECT 8 unique users, then SELECT all records within those 8 users, then SELECT 4 most recent and available records of each 8 users?

I have a database (dates are just examples for order sake)...

---------------------
| user | item |  date |
 ---------------------
|    1 |    a |   123 |
|    3 |    b |   124 |
|    1 |    c |   125 |
|    2 |    d |   126 |
|    5 |    i |   127 |
|    4 |    e |   128 |
|    6 |    f |   129 |
|    9 |    g |   130 | 
|    3 |    h |   131 |
|    9 |    s |   132 |
|    1 |    j |   133 |
|    2 |    k |   134 |
|    1 |    l |   135 |
|    1 |    m |   136 |
|    1 |    n |   137 |
|    8 |    o |   138 |
|    5 |    p |   139 |
|    9 |    q |   140 |
|    7 |    r |   141 |
 ---------------------

I would like to get all records up to the first 8 unique users, which would make the results...

---------------------
| user | item |  date |
 ---------------------
|    1 |    a |   123 |
|    3 |    b |   124 |
|    1 |    c |   125 |
|    2 |    d |   126 |
|    5 |    i |   127 |
|    4 |    e |   128 |
|    6 |    f |   129 |
|    9 |    g |   130 | 
|    3 |    h |   131 |
|    9 |    s |   132 |
|    1 |    j |   133 |
|    2 |    k |   134 |
|    1 |    l |   135 |
|    1 |    m |   136 |
|    1 |    n |   137 |
|    8 |    o |   138 |
 ---------------------

Then from those records, I'd like to get the most recent 4 records per unique user , making the results look like...

---------------------
| user | item |  date |
 ---------------------
|    3 |    b |   124 |
|    2 |    d |   126 |
|    5 |    i |   127 |
|    4 |    e |   128 |
|    6 |    f |   129 |
|    9 |    g |   130 | 
|    3 |    h |   131 |
|    9 |    s |   132 |
|    1 |    j |   133 |
|    2 |    k |   134 |
|    1 |    l |   135 |
|    1 |    m |   136 |
|    1 |    n |   137 |
|    8 |    o |   138 |
 ---------------------

Ideally I would be able to do this with one query. The closest I've been able to come is with this query:

SELECT users, 
GROUP_CONCAT(items) 
FROM db 
GROUP BY users 
ORDER BY date 
LIMIT 8

But GROUP_CONCAT gives back all results for that user, not just the amount in the selection.

I've also tried...

SELECT users
FROM db AS u1
JOIN (SELECT DISTINCT users FROM db) AS u2 ON u1.users = u2.users

from another suggestion I found but this also didn't work.

I've tried a ton of other things that I didn't really save because they didn't work and I was pretty confident I'd figure it out, but it's been two weeks and I haven't got close. If any SQL gurus are out there that can point me in the right direction, that would be really great. Thanks.

Hoping this can help you.

--get all records up to the first 8 unique users,depending on there first order date
 select distinct a.user,a.date as first_order_date from yourtable as a where 
 a.date = (select MIN(date) from yourtable as b where a.user=b.user) order by a.date LIMIT 8

Then using above result to get the most recent 4 records per unique user like below:

select * from yourtable as t where t.date in 
(select date from yourtable as t2 where t.user=t2.user order by t2.date desc LIMIT 4 ) and 
t.user in (select distinct a.user,a.date as first_order_date from yourtable as a
where a.date = (select MIN(date) from yourtable as b where a.user=b.user) order by a.date LIMIT 8)

References:

correlated subqueries

Example

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