Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL说:错误1064-在sql中使用IF条件时_Mysql_Sql_Database - Fatal编程技术网

MySQL说:错误1064-在sql中使用IF条件时

MySQL说:错误1064-在sql中使用IF条件时,mysql,sql,database,Mysql,Sql,Database,我在sql命令中得到了这个错误消息。我无法找出原因。请帮帮我 if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then begin UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1' end ; else begin INSERT I

我在sql命令中得到了这个错误消息。我无法找出原因。请帮帮我

if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
begin 
UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
end ;
else
begin
INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
end;
end if; 
在这里,如果设置为1,我将尝试更新列current,否则将插入一条新记录

提交表单时,我需要在php文件中使用此sql命令

以下是错误消息:


1064-您的SQL语法有错误

这不是mysql中的有效语句,有效语法如下所示。另外,如果存储过程、函数或触发器中允许else

if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
 begin 
   UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
 end ;
else
 begin
    INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
 end;
end if; 

整个语句集只需一次插入即可完成…在重复密钥更新时。。。语句。

什么错误?这是一个sql语句…据我所知,在MySQL中,您只能在以下情况下使用。。在存储过程或函数中。您也不能以这种方式使用EXISTS,您需要使用IF SELECT EXISTS SELECT*FROM teams-有关更多详细信息,请参阅。ok@GarethD,我正在更新它。嗯,我的sql不是很好;请提供一个示例或查询。代码是存储过程的一部分,还是您正在其他地方尝试?嘿@Abhik,我在sql方面非常弱,所以我对存储过程一无所知。提交表单时,我需要在php文件中使用上述sql命令。但是我试图在phpmyadmin中直接使用它进行测试。不,这在通过PHP的常规mysql中不起作用。如果然后开始。。。它们是存储过程或触发器的语法。您不能直接在mysql中使用它。ok@Abhik。那么,如何在我的php文件中执行相同的操作呢。有什么建议吗?您需要使用PHP创建一个produre,然后执行它。在这里查看教程