MySQL 根据其原始值更改值
大家好, 长期读者,第一次海报。
这听起来应该很简单,但我在任何地方都找不到解决方案。我正在建立一个评级系统,人们可以对某些内容是否活跃进行评级。它有自己的小逻辑,但为了让它发挥作用,我需要这样做。
- 根据当前评级检查项目评级,
- 将其更改为预设金额。
我可以使用两个 SQL 语句在 PHP 中对其进行硬编码,但我确信使用单个存储过程(一个用于投票赞成,另一个用于投票反对)会快得多。
示例表:
item_id | item_rating
---------------------
10 | 1
向上投票 item_ rating 的逻辑:
if | then
---------
0 | 1
1 | 2
-1 | 1
-2 | 1
2 | 2
向下投票 item_ rating 的逻辑:
if | then
---------
0 | -1
1 | -1
-1 | -2
-2 | -2
2 | -1
我知道基于简单点的系统会更容易,但由于系统的性质,这是我能找到的最简单的解决方案。
有人可以解释一下我如何在 SQL 中使用 IF 语句来实现这一点吗?我确信对于知情者来说答案是显而易见的。
(顺便说一句,使用最新版本的MySQL)
Hy everyone,
Long time reader, first time poster.
This sounds like it should be really simple but I can't find the solution anywhere. I'm building a ratings system where people can rate if something is active or not. It has its own little logic but for it to work I need to.
- check the items rating
- depending on the current rating change it to a pre set amount.
I could hard code it in PHP with two SQL statements but I'm sure using a single stored procedure (one for vote up, another for vote down) will be much faster.
example table:
item_id | item_rating
---------------------
10 | 1
logic to vote item_rating up:
if | then
---------
0 | 1
1 | 2
-1 | 1
-2 | 1
2 | 2
logic to vote item_rating down:
if | then
---------
0 | -1
1 | -1
-1 | -2
-2 | -2
2 | -1
I know a simple points based system would be easier but due to the nature of the system this is the simplest solution I could find.
Can someone explain how I would use IF statements in SQL to achieve this? I'm sure the answer is really obvious to someone in the know.
(btw using the latest version of MySQL)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您要找的吗?这是一个赞成票:
这是一个反对票:
Is this what you're looking for? Here's an upvote:
Here's a downvote:
这是未经测试的,但我认为它应该有效。
This is untested, but I think it should work.