如何在 MySQL 查询中编写 IF ELSE 语句

2021-11-20 00:00:00 if-statement mysql

如何在 MySQL 查询中编写 IF ELSE 语句?

How do I write an IF ELSE statement in a MySQL query?

像这样:

mysql_query("...(irrelevant code).. IF(action==2&&state==0){state=1}");

然后在我的数组中我应该能够做到这一点:

Then down in my array I should be able to do this:

 $row['state'] 
//this should equal 1, the query should not change anything in the database, 
//just the variable for returning the information

推荐答案

您可能想要使用 CASE 表达式.

You probably want to use a CASE expression.

它们看起来像这样:

SELECT col1, col2, (case when (action = 2 and state = 0) 
 THEN
      1 
 ELSE
      0 
 END)
 as state from tbl1;

相关文章