Sql COALESCE entire rows?

前端 未结 4 1917
南笙
南笙 2021-01-18 08:21

I just learned about COALESCE and I\'m wondering if it\'s possible to COALESCE an entire row of data between two tables? If not, what\'s the best approach to the following r

4条回答
  •  囚心锁ツ
    2021-01-18 08:49

     SELECT Id, Name, Email, Etc FROM tbl_Employees
          WHERE Id IN (SELECT ID From tbl_PeopleInID)
     UNION ALL
     SELECT Id, Name, Email, Etc FROM tbl_Customers
          WHERE Id IN (SELECT ID From tbl_PeopleInID) AND
                Id NOT IN (SELECT Id FROM tbl_Employees)
    

    Depending on the number of rows, there are several different ways to write these queries (with JOIN and EXISTS), but try this first.

    This query first selects all the people from tbl_Employees that have an Id value in your target list (the table tbl_PeopleInID). It then adds to the "bottom" of this bunch of rows the results of the second query. The second query gets all tbl_Customer rows with Ids in your target list but excluding any with Ids that appear in tbl_Employees.

    The total list contains the people you want — all Ids from tbl_PeopleInID with preference given to Employees but missing records pulled from Customers.

提交回复
热议问题