如何在MySQL查询中编写IF ELSE语句?
如下所示:
mysql_query("...(irrelevant code).. IF(action==2&&state==0){state=1}");
然后在我的数组中,我应该能够这样做:
$row['state']
//this should equal 1, the query should not change anything in the database,
//just the variable for returning the information
发布于 2012-01-07 03:32:05
您可能想要使用CASE
expression。
它们看起来像这样:
SELECT col1, col2, (case when (action = 2 and state = 0)
THEN
1
ELSE
0
END)
as state from tbl1;
发布于 2012-01-07 03:31:47
你必须用SQL编写它,而不是用C/PHP风格
IF( action = 2 AND state = 0, 1, 0 ) AS state
在查询中使用
IF ( action = 2 AND state = 0 ) THEN SET state = 1
在存储过程或函数中使用
发布于 2012-01-07 03:29:50
你在找case
case when action = 2 and state = 0 then 1 else 0 end as state
MySQL具有if
语法(if(action=2 and state=0, 1, 0)
),但case
更通用。
请注意,那里的as state
只是为列添加了别名。我假设它在您的SQL查询的列列表中。
https://stackoverflow.com/questions/8763310
复制相似问题