skip to Main Content

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"

2

Answers


  1. You may need to use CASE statement.

    SELECT Name, LastName, (case when (Name is null)
    THEN
        LastName
    ELSE
        Name
    END
    )
    as data from Table2;
    
    Login or Signup to reply.
  2. Try with COALESCE:

    SELECT 
      `ID`,
      COALESCE(`Name`, `LastName`) AS `Name`
    FROM 
      `Table2`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search