Home > Enterprise >  What is the equivalent function in Oracle for IF function in MySQL?
What is the equivalent function in Oracle for IF function in MySQL?

Time:06-10

so I have this function in mysql syntax(on a mysql DB) that I'm trying to run on Oracle and can't seem to find the similar function in Oracle for MYSQL "IF".

SELECT 
table1.PROJECT_ID
max(if(Table2.Typ = 'progress', 'progress',NULL)) AS 'progress',
max(if(Table2.Typ = 'acquired', 'acquired',NULL)) AS 'acquired'
FROM Table1
LEFT JOIN Table2 ON Table2.Name = Table1.Item_Description
GROUP BY PROJECT_ID;

CodePudding user response:

Use a CASE expression (which is in the ANSI standard):

SELECT table1.PROJECT_ID,
       max(CASE WHEN Table2.Typ = 'progress' THEN 'progress' ELSE NULL END) AS progress,
       max(CASE WHEN Table2.Typ = 'acquired' THEN 'acquired' ELSE NULL END) AS acquired
FROM   Table1
       LEFT JOIN Table2 ON Table2.Name = Table1.Item_Description
GROUP BY PROJECT_ID;
  • Related