mysql - 简单的 IF.. Else.. mysql 查询

标签 mysql sql

set @var=1;
if @var>1 then 
select * from client;
else
select * from otherTable;
end if;

这是我的 mysql 查询。你们能指出为什么显示错误吗? 这真是伤透我的脑筋了。

显示的错误是

IF @var >1 THEN SELECT *
FROM client;

MySQL said: Documentation
#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 @var>1 then
select * from client' at line 1 

快速回答会非常有帮助。

最佳答案

在 MySQL 中,您必须在触发器或过程中执行它。

您不能只运行没有函数的脚本代码。

delimiter |
CREATE PROCEDURE simpleproc ()
BEGIN
   set @var=1;
   if @var>1 then 
      select * from client;
   else
      select * from otherTable;
   end if;
END;
|
delimiter ;

之后你可以执行它

call simpleproc()

关于mysql - 简单的 IF.. Else.. mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18716425/

相关文章:

java.lang.IllegalAccessError : org. apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z

mysql - 在其他查询中使用派生子查询结果

具有基于多行的条件的MySQL查询

sql - 'ORA-00918: column ambiguously defined'如何解决

java - 的用法?在准备好的声明中

sql - N :M Relationship between users and guests

php - 如何将 Count (*) 作为 SQL 到 php 的变量?

mysql - 我可以将此模式用于 where 子句的可选部分吗?

sql - 创建 MySQL 字符串 "safe"(转义双引号)

c# - MySql查询合并表并填充dataGridView