Search code examples
sqlsql-serversql-server-2014

Search by results of multiple columns


What is correct way to use query like this one?

 SELECT Project_name,
  FROM [Table 1]
  WHERE Project_id in
      (SELECT ID_1, ID_2, ID_3, ID_4, ID_5
       FROM [Table 2])

i.e. to search by select result of 2 and more columns


Solution

  • Using apply:

      SELECT Project_name,
      FROM [Table 1]
      WHERE Project_id in
      (SELECT v.value FROM [Table 2]
       cross apply (values(ID_1), 
                          (ID_2), 
                          (ID_3), 
                          (ID_4), 
                          (ID_5))v(value))