Cómo escribir la sentencia IF ELSE en una consulta MySQL


¿Cómo escribo una sentencia IF ELSE en una consulta MySQL?

Algo como esto:

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

Entonces abajo en mi arreglo debería ser capaz de hacer esto:

 $row['state'] 
//this should equal 1, the query should not change anything in the database, 
//just the variable for returning the information
Author: Eric Leschinski, 2012-01-06

5 answers

Es probable que desee utilizar un CASE expresión .

Se ven así:

SELECT col1, col2, (case when (action = 2 and state = 0) 
 THEN
      1 
 ELSE
      0 
 END)
 as state from tbl1;
 128
Author: Jack Edmonds,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-15 11:59:33

Debe escribirlo en SQL no en el estilo C / PHP

IF( action = 2 AND state = 0, 1, 0 ) AS state

Para su uso en consulta

IF ( action = 2 AND state = 0 ) THEN SET state = 1

Para su uso en procedimientos o funciones almacenados

 20
Author: SergeS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-06 19:31:46

Estás buscando case:

case when action = 2 and state = 0 then 1 else 0 end as state

MySQL tiene una sintaxis if (if(action=2 and state=0, 1, 0)), pero case es más universal.

Tenga en cuenta que el as state no es solo aliasing la columna. Asumo que esto está en la lista de columnas de su consulta SQL.

 14
Author: Eric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-06 19:29:49
SELECT col1, col2, IF( action = 2 AND state = 0, 1, 0 ) AS state from tbl1;

O

SELECT col1, col2, (case when (action = 2 and state = 0) then 1 else 0 end) as state from tbl1;

Ambos resultados serán iguales....

 12
Author: Khandad Niazi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-21 13:38:01

De acuerdo con el manual de referencia de MySQL, esta es la sintaxis de usar la instrucción if y else:

IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF

Así que con respecto a su consulta:

x = IF((action=2)&&(state=0),1,2);

O puede usar

IF ((action=2)&&(state=0)) then 
state = 1;
ELSE 
state = 2;
END IF;

Hay un buen ejemplo en este enlace : http://easysolutionweb.com/sql-pl-sql/how-to-use-if-and-else-in-mysql /

 -1
Author: user2613580,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-25 12:54:48