简体   繁体   English

高级 MySQL 查询、选择、计数、插入或更新

[英]Advanced MySQL query, select, count, insert or update

So, I have a query, what it needs to do is;所以,我有一个查询,它需要做的是;

  • Check if a Token exist in the sessions table检查会话表中是否存在令牌
  • If not insert a token in the table如果没有在表中插入一个令牌
  • Else update the token in the table否则更新表中的令牌

SELECT COUNT(token) AS founds FROM sessions WHERE token = '" . $_SESSION['prevToken'] . "'
CASE WHEN founds == 0 THEN
    INSERT INTO sessions(token, cookie, expire)
    VALUES('" . $token . "', '', NOW() + INTERVAL 1 HOUR))
ELSE 
    UPDATE sessions SET token = '" . $token . "', expire = NOW() + INTERVAL 1 HOUR)
    WHERE  token = '" . $_SESSION['prevToken'] . "'
END CASE

But I get this error:但我收到此错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN founds == 0 THEN INSE' at line 2

And I don`t know whats wrong?我不知道怎么了? The CASE syntax should be right but it isn't? CASE 语法应该是正确的,但它不是?

You are using CASE WHEN.. as if it were a control structure.您正在使用CASE WHEN..就好像它是一个控制结构一样。 It isn't.不是。 It's used in queries.它用于查询。 Apart from that, you can't use control structures like if or while in a query, those are only allowed in stored procedures or functions.除此之外,您不能在查询中使用ifwhile等控制结构,这些仅允许在存储过程或函数中使用。

Also you have no delimiter after your select query.您的select查询后也没有分隔符。

Clément Malet's answer is right, that == is not used in MySQL. Clément Malet 的回答是正确的,即 MySQL 中不使用==

Best solution would be indeed to useINSERT ... ON DUPLICATE KEY UPDATE like in LHristov's answer.最好的解决方案确实是使用INSERT ... ON DUPLICATE KEY UPDATE就像在 LHristov 的回答中一样。

Apart from that, just for completeness, here's how a correct stored procedure would look like:除此之外,为了完整起见,以下是正确的存储过程的样子:

DELIMITER $$
CREATE PROCEDURE insert_or_update(IN p_prev_token varchar(255), IN p_token varchar(255))
BEGIN
  IF EXISTS( SELECT 1 FROM sessions WHERE token = p_prev_token ) THEN
    UPDATE sessions SET token = p_token, expire = NOW() + INTERVAL 1 HOUR)
    WHERE  token = p_prev_token;
  ELSE
    INSERT INTO sessions(token, cookie, expire)
    VALUES(p_token, '', NOW() + INTERVAL 1 HOUR));
  END IF;
END $$
DELIMITER ;

You would then call it with something like this (I'm no PHP programmer, therefore I don't know how to escape the variables or whatever):然后你会用这样的东西调用它(我不是 PHP 程序员,因此我不知道如何转义变量或其他什么):

CALL insert_or_update($_SESSION['prevToken'], $token);

PS: And in general don't COUNT() when all you need to know is, if there's an entry. PS:通常不要COUNT()当你需要知道的是,如果有一个条目。 EXISTS() stops as soon as the first entry is found, whereas COUNT() continues to scan the table. EXISTS()一旦找到第一个条目就停止,而COUNT()继续扫描表。

So you need INSERT ... ON DUPLICATE KEY UPDATE .所以你需要INSERT ... ON DUPLICATE KEY UPDATE Get some info fromdocumentation文档中获取一些信息

You can't use '==' in MySQL.你不能在 MySQL 中使用 '=='。

Replace WHEN founds == 0 with WHEN founds = 0WHEN founds == 0替换为WHEN founds = 0

you also can use你也可以使用

ON DUPLICATE KEY UPDATE

and not use case.而不是用例。 ex:前任:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

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

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