I'm making an web with point and reward system where user collect points and if users point meet the requirement, user will get a reward and level up.
As example, here's my user table
id | username | level | point
1 | nameuser | 0 | 0
Award :
idaward | awardname | pointreq | badgeid
1 | LEVEL ONE | 10 | 1
and so on, I have 100+ award for user.
Award log
idlog | userid | awardid | badgeid | givendate
And my purpose is how to check if user is eligible or meet the requirement to get an award & badge ?
The flow:
Users have a point -> check if user meets the requirement to level up & get award
-> Users will leveled up and get a reward (insert the info. to award log)
If users already have it then ignore it.
I'm using Laravel, so if you can make the Laravel query, I will really appreciate it but I will also accept raw MySQL query.
Thank you
You can execute the following update query:
UPDATE user
SET level = level + 1
WHERE
id = ?
AND EXISTS (
SELECT * FROM award WHERE pointreq <= user.point
AND awardid NOT IN (
SELECT awardid from awardlog WHERE userid = user.id
)
);
If the update returns 1 then you need to insert an entry into awardlog table, you can use following SELECT
and INSERT
queries for it:
SELECT * FROM award WHERE pointreq <= user.point
AND awardid NOT IN (
SELECT awardid from awardlog WHERE userid = user.id
);
You can get awardid and badgeid from above query and insert into awardlog.
Also, you need to make sure all 3 queries ( UPDATE
, SELECT
and INSERT
) get executed in a single transaction for consistency.
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.