Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL 5.7 nested variable question, please help #1

Open
johnhhu2020 opened this issue Jan 6, 2023 · 1 comment
Open

MySQL 5.7 nested variable question, please help #1

johnhhu2020 opened this issue Jan 6, 2023 · 1 comment

Comments

@johnhhu2020
Copy link

RESET QUERY CACHE;

SELECT vip.user_id id
FROM (SELECT oo.user_id, IF(SUM(IF(oo.Rev >= 40, 1, 0)) >= 3, 1, 0) SSVIP_Seg,
IF(SUM(IF(oo.Rev < 40 AND oo.Rev >= 15, 1, 0)) >= 3, 1, 0) SVIP_Seg
FROM (SELECT kk.id, kk.user_id, kk.Rev, kk.created_at, kk.rn, IF(@prev1 <> kk.user_id, @RN1:=0, @RN1), @prev1:=kk.user_id, @RN1:=@RN1+1 AS rn1
FROM (SELECT pp.id, pp.user_id, pp.Rev, pp.created_at, pp.rn
FROM (SELECT up.id, up.user_id, created_at, ROUND(amountpayout_foreign_exchange_rate) Rev, IF(@Prev <> user_id, @rn:=0, @rn), @Prev:=user_id, @rn:=@rn+1 AS rn
FROM (SELECT id, user_id, amount, payout_foreign_exchange_rate, created_at
FROM XXXYYY.user_purchase
WHERE status = 'completed'
AND created_at >= NOW() -INTERVAL 60
86400 SECOND
AND user_id IS NOT NULL
AND user_id > 0
ORDER BY user_id, id) up, (SELECT @rn:=0) rn, (SELECT @Prev:='') prev) pp
JOIN (SELECT pp.user_id, MAX(pp.rn) Max_rn
FROM (SELECT up.id, up.user_id, IF(@Prev <> user_id, @rn:=0, @rn), @Prev:=user_id, @rn:=@rn+1 AS rn
FROM (SELECT id, user_id
FROM XXXYYY.user_purchase
WHERE status = 'completed'
AND created_at >= NOW() -INTERVAL 6086400 SECOND
AND user_id IS NOT NULL
AND user_id > 0
ORDER BY user_id, id) up, (SELECT @rn:=0) rn, (SELECT @Prev:='') prev) pp
GROUP BY pp.user_id) jk
ON pp.user_id = jk.user_id AND pp.rn >= jk.Max_rn - 6 AND jk.Max_rn - 6 >= 1 /
<2>WHERE pp.user_id = 240XX24*/) kk, (SELECT @RN1:=0) rn1, (SELECT @prev1:='') prev1
WHERE user_id IN (/250XX24,/ 240XX24)
/* <1> Here you cant limit only 1 user, ???????????????????????? */
) oo
GROUP BY oo.user_id) vip
WHERE vip.SVIP_Seg = 0 AND vip.SSVIP_Seg = 0;

If you are doing the limit in <1> with one user, the order will be miss sorted as below:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
id, user_id, Rev, created_at, rn, IF(@prev1 <> kk.user_id, @RN1:=0, @RN1), @prev1:=kk.user_id,rn1
29225 | 240XX24 | XX.0 | 2022-11-08 13:43:32.0 | 2.0 | 0 | 2405724 | 1.0
29321 | 240XX24 | XX.0 | 2022-11-10 20:40:49.0 | 3.0 | 1 | 2405724 | 2.0
29930 | 240XX24 | XX.0 | 2022-11-24 05:03:30.0 | 4.0 | 2 | 2405724 | 3.0
30408 | 240XX24 | XX.0 | 2022-12-16 04:02:46.0 | 5.0 | 3 | 2405724 | 4.0
30460 | 240XX24 | XX.0 | 2022-12-18 06:29:23.0 | 6.0 | 4 | 2405724 | 5.0
30483 | 240XX24 | XX.0 | 2022-12-18 23:49:47.0 | 7.0 | 5 | 2405724 | 6.0
29176 | 240XX24 | XX.0 | 2022-11-07 02:38:24.0 | 1.0 | 6 | 2405724 | 7.0
^ ^
But you can do the limitation in <2>, I want to know why such thing happened, would you mind explain the machinasm behind it, I was read somewhere that Mariadb team also created MySQL many years ago.

@johnhhu2020
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant