“如果存在”的 SQL 语法

发布于 2024-09-09 17:17:52 字数 353 浏览 9 评论 0原文

为什么我收到此错误:

#1064 - 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 
'IF EXISTS(SELECT id FROM mytable WHERE id = '1')' at line 1 

我的 SQL 查询:

IF EXISTS(SELECT id FROM mytable WHERE id = '1')

谢谢。

Why I'm getting this error:

#1064 - 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 
'IF EXISTS(SELECT id FROM mytable WHERE id = '1')' at line 1 

My SQL query:

IF EXISTS(SELECT id FROM mytable WHERE id = '1')

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦境 2024-09-16 17:17:52

IF EXISTS 仅适用于存储过程。在存储过程之外,IF() 是一个带有 3 个参数的函数。正确的用法是

SELECT IF(EXISTS(SELECT `column` FROM `table` WHERE `id` = `1`), 1, 0);

IF EXISTS only works in a stored procedure. Outside of a stored procedure, IF() is a function which takes 3 arguments. Proper usage would be

SELECT IF(EXISTS(SELECT `column` FROM `table` WHERE `id` = `1`), 1, 0);
故人爱我别走 2024-09-16 17:17:52

现在不在 MySQL 机器上,但看起来是这样,因为语句不完整,您需要告诉它如果 id 存在该怎么办。

IF EXISTS(...) 做某事

Not on a MySQL machine right now but it looks like its because the statement is incomplete, you need to tell it what to do if the id exists.

IF EXISTS(...) do something

森林迷了鹿 2024-09-16 17:17:52

IF EXISTS 在 MySQL 中没有任何意义。
请参阅带有 EXISTS 或 NOT EXISTS 的子查询< /a> 有关用法的 MySQL 文档。

基本上,您需要在语句中使用它,而不仅仅是像逻辑块一样

IF EXISTS doesn't make any sense in MySQL.
See subqueries with EXISTS or NOT EXISTS in the MySQL documentation on usage.

Basically, you need to use it in a statement, and not just like a logical block

厌味 2024-09-16 17:17:52

尝试使用

Declare @ID Integer
Select @ID=id From mytable where id=1
IF @ID is not null    OR  IF @ID > 0
Begin
....
End

Try to use

Declare @ID Integer
Select @ID=id From mytable where id=1
IF @ID is not null    OR  IF @ID > 0
Begin
....
End
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文