How do write IF ELSE statement in a MySQL query(如何在 MySQL 查询中编写 IF ELSE 语句)
问题描述
如何在 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;
这篇关于如何在 MySQL 查询中编写 IF ELSE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!