如何在 Mysql 中使用 IF() 更改两个单词之间的值?
How to change a value between 2 words with IF() in Mysql?
我正在尝试使用以下代码在 table 中更改开/关之间的值:
$sql = "UPDATE example SET IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);
但它无法正常工作,我收到此错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF(statuss = 'on','off','on') WHERE id = '16'' at line 1
您缺少对字段名称的分配。
试试这个:
$sql = "UPDATE example SET status = IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);
我正在尝试使用以下代码在 table 中更改开/关之间的值:
$sql = "UPDATE example SET IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);
但它无法正常工作,我收到此错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF(statuss = 'on','off','on') WHERE id = '16'' at line 1
您缺少对字段名称的分配。
试试这个:
$sql = "UPDATE example SET status = IF(status = 'on','off','on') WHERE id = '16'";
$connect->exec($sql);