Mysql query with if else statements

I have the following fields in my ad_table :

+----------------------------------------------------------+
|ADVT_ID | RELATIVE_ID | AD_TYPE | AD_LOCATION | AD_IMAGE  |
+----------------------------------------------------------+
|1       | 2            |  FP    | H           | test1.jpg |
|2       | 0            |  FP    | H           | test2.jpg |
|3       | 0            |  TB    | H           | test3.jpg |
+----------------------------------------------------------+

I want to use two if statements in a mysql query. I want to get data from a table using mysql query in this format:

FIRST IF STATEMENT:

if (AD_TYPE = 'FP' && AD_LOCATION = 'H')   
then select all the data from   
where RELATIVE_ID! = 0

SECOND IF STATEMENT:

if (AD_TYPE! = 'FP' && AD_LOCATION! = 'H')   
then select all the data from the
table where RELATIVE_ID = 0

So how to do this in mysql query?

+4
source share
1 answer

You can try:

SELECT *
FROM ad_table
WHERE (AD_TYPE = 'FP' AND AD_LOCATION = 'H' AND RELATIVE_ID !=0)
   OR (AD_TYPE != 'FP' AND AD_LOCATION != 'H' AND RELATIVE_ID =0)
+4
source

Source: https://habr.com/ru/post/1538355/


All Articles