Skip to content
Advertisement

MySQL WHERE with conditions/priority condtion

This one is a weird one that I just can’t seem to wrap my head around.

Basically I need to search based on a value that has an override. So there are 2 columns with essentially the same content. One is updated automatically and one is manual. If the automatic one is not available it will use the manual, and if the manual is not available it will use the automatic one. Lastly if both are available it will use the automatic unless and “override” flag is enabled (probably a third column).

What I need to do is search based on that parameter as if it were one field, using the set of priorities on the columns. So here is an example WHERE statement, but I do not know what might be the correct syntax to use.

WHERE (isset(width)?(width_override?o_width:width):o_width) = {query}

Advertisement

Answer

Use the IF function.

WHERE IF(width IS NOT NULL, IF(width_override, o_width, width),
                            o_width) = {query}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement