4

I have following fields in my ad_table 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 mysql query. I want to retrieve data from table using mysql query in this format:

FIRST IF STATEMENT :

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

SECOND IF STATEMENT :

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

So how to do that in mysql query?

2
  • Why do this in mysql? Use php to process which query you will call. You should always limit the number of calls to the MYSQL servers and offload as much processing on the application (php) side. Of course, you might not be able to do that... but this is the ideal setup. Commented Apr 28, 2014 at 8:06
  • Are you talking about the IF flow control statement or the IF() function? The first one is only available in procedures and functions and you don't mention you're writing one. The function, of course, is not a flow control structure. Also, what does the PHP tag have to do with your question? Commented Apr 28, 2014 at 8:10

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

8 Comments

i want to do something like that if(AD_TYPE = 'FP' AND AD_LOCATION = 'H') then select all data where RELATIVE_ID !=0 if(AD_TYPE != 'FP' AND AD_LOCATION != 'H') then select all data where RELATIVE_ID =0
"if(AD_TYPE = 'FP' AND AD_LOCATION = 'H')", but for which line ? These values are not constant over your table.
i want to use two if statements in mysql query
First IF :if(AD_TYPE = 'FP' AND AD_LOCATION = 'H') then select all data where RELATIVE_ID !=0 SECOND IF :if(AD_TYPE != 'FP' AND AD_LOCATION != 'H') then select all data where RELATIVE_ID =0
You don't understand.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.