How can I use something like an IF in an SQL query

Asked

Viewed 161 times

1

I am applying a query SQL in the Oracle SQL Developer in multiple tables where a Column a view is null in some places and then I will need to use values from another Column and I don’t know how to use the query’s IF very well.

Can anyone help with some possible solutions?

E.g.

SELECT x.Column1 "Alias",
       y.Column2 "Alias2",
FROM table x
INNER JOIN table y
ON (IF x.Column3 or x.Column4) = y.Column2 
.
.
.
  • 1

    Are you using Oracle or Mysql? Described one and tagged another.

  • Opa thanks for the warning, I freaked out sorry

2 answers

3


A solution on Oracle

SELECT x.Column1 "Alias",
       y.Column2 "Alias2",
FROM table x,table y
ON NVL(x.Column3,x.Column4) = y.Column2 

But in general problems of this kind advise a revision of the Model.

  • Thanks for the tip, revise the template is a job I can not have now rsrsr but I will consider with total certainty! have a good day

1

Mysql can be used as follows, as it was almost already doing:

SELECT x.Column1 "Alias",
       y.Column2 "Alias2",
FROM table x
INNER JOIN table y
ON (x.Column3 = y.Column2 OR x.Column4 = y.Column2)
  • Thanks for the solution I will test it and find out which one best solves, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.