如何在 MySQL 查询中编写 IF ELSE 语句 [英] How do write IF ELSE statement in a MySQL query

查看:71
本文介绍了如何在 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 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆