Using an IF Statement in a MySQL SELECT query

前端 未结 3 1883
无人及你
无人及你 2020-11-27 18:30

I am trying to use an IF statement in a MySQL select query.

I am getting an error after the AND statement where the first IF

3条回答
  •  囚心锁ツ
    2020-11-27 19:12

    The IF/THEN/ELSE construct you are using is only valid in stored procedures and functions. Your query will need to be restructured because you can't use the IF() function to control the flow of the WHERE clause like this.

    The IF() function that can be used in queries is primarily meant to be used in the SELECT portion of the query for selecting different data based on certain conditions, not so much to be used in the WHERE portion of the query:

    SELECT IF(JQ.COURSE_ID=0, 'Some Result If True', 'Some Result If False'), OTHER_COLUMNS
    FROM ...
    WHERE ...
    

提交回复
热议问题