0

I would like to use if statement in sql query :

what I want :

if(tractions_delivery.send_date_id !=0 ){

           date_send_commodities.id  = tractions_delivery.send_date_id
}

my query :

        from

            tractions_delivery,user_address,province,city,date_send_commodities,users

        WHERE

        tractions_delivery.tr_id = $tr_id
        AND
        tractions_delivery.address_id = user_address.id
        AND
        user_address.province_id = province.id
        AND
        user_address.city_id      = city.id
        AND
        //not work
        (tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id  = tractions_delivery.send_date_id)

        AND
        users.id = user_address.user_id
6
  • Can you clarify what this query is supposed to do please? Commented Jan 18, 2016 at 9:04
  • 2
    ... AND CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND users.id = user_address.user_id Commented Jan 18, 2016 at 9:05
  • What is IS NOT 0? You can write IS NOT NULL or <> 0. Commented Jan 18, 2016 at 9:13
  • @S.M_Emamian :Try this: IF(tractions_delivery.send_date_id <> 0, date_send_commodities.id, <some_other_column>) = tractions_delivery.send_date_id Commented Jan 18, 2016 at 9:25
  • @iiro your answer is true and work fine. Commented Jan 18, 2016 at 10:01

3 Answers 3

1

You could use the CASE-statement

SELECT 
*
FROM
 tractions_delivery,
 user_address,
 province,
 city,
 date_send_commodities,users
WHERE
 tractions_delivery.tr_id = $tr_id AND
 tractions_delivery.address_id = user_address.id AND
 user_address.province_id = province.id AND
 user_address.city_id = city.id AND
 CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
 users.id = user_address.user_id
Sign up to request clarification or add additional context in comments.

Comments

1

You can only use if statements in stored procedures or functions. If you just write a sql statement unfortunately you cannot use if statements around the query. But you can use logic in the query itself, e.g.:

SELECT CASE WHEN col1 = col2 THEN'col1 equals col2' else 'col1 doesnt equal col2' ELSE
FROM table1

So around doesnt work, but in the field list you can create CASE WHEN ELSE END logic.

Comments

1

CASE or IF() operators can be of help.

Examples,

SELECT (CASE 1 WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE 'More' END) 'Result';

OR

SELECT IF(1=1, 'One', 'Two') 'Result';

These CASE and IF() operators can be used in the SELECT clause to conditionally interpret column values and return in the resultset.

Note: Do not confuse CASE operator here with 'CASE conditional syntax block' that ends with END CASE.

Comments

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.