I need a query where if a column from a row was filled, return her, else return another column value in the same table. This code will insert like a subquery, so, I need that return just only value. ** Data Base Exemple**
ID | Name | LastName |
---|---|---|
1 | Maria | Nunes |
2 | Null | Torres |
Expected query
SELECT *,
(*new subquery FROM TableName) AS Name,
FROM Table2.
Expected response
ID | Name |
---|---|
1 | Maria |
2 | Torres |
Actually, I get the two values from columns and mount an object with "if statement" separately to check if value is difference of "null"
CodePudding user response:
You may need to use CASE statement.
SELECT Name, LastName, (case when (Name is null)
THEN
LastName
ELSE
Name
END
)
as data from Table2;
CodePudding user response:
Try with COALESCE
:
SELECT
`ID`,
COALESCE(`Name`, `LastName`) AS `Name`
FROM
`Table2`