简体   繁体   English

检查用户是为了满足MySQL的要求

[英]Checking the users is for meeting the requirement on MySQL

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. 等等,我有100多个用户奖。

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. 我正在使用Laravel,所以如果你可以进行Laravel查询,我会非常感激,但我也会接受原始的MySQL查询。

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: 如果更新返回1,那么您需要在awardlog表中插入一个条目,您可以使用以下SELECTINSERT查询:

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. 您可以从上面的查询获得awardid和badgeid并插入奖励日志。

Also, you need to make sure all 3 queries ( UPDATE , SELECT and INSERT ) get executed in a single transaction for consistency. 此外,您需要确保在单个事务中执行所有3个查询( UPDATESELECTINSERT )以保持一致性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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