JC Borlagdan JC Borlagdan - 7 months ago 7
SQL Question

Is this query possible? SELECT FROM 2 tables LIKE

I know this will cause an ambiguous error since Name exists in both tables. But
I want to select the Name from these two tables. Name is a column in t_1 and t_2. Is it possible to select from two tables? Is there a way for me to select the name between two tables, and returns a single column of data?

SELECT Name
FROM t_1, t_2
WHERE Name LIKE '%' + @USER + '%'

Answer

You could try a UNION;

SELECT Name
FROM t_1
WHERE Name LIKE '%' + @USER + '%'
UNION
SELECT Name
FROM t_2
WHERE Name LIKE '%' + @USER + '%'

Note, UNION ALL can also be used, this will also return multiple instances of the same name if they exist. Use whichever satisfies your requirements.

Comments