gymcode
gymcode

Reputation: 4633

SQL SELECT statement from both tables

I have 2 select statements which I want to grep the results of both table and display then a dropdown box. I am able to do it for each SELECT statement but I am unable to select from both at the same time.

Below are my 2 SELECT statements:

string loggedIn= (User.Identity.Name);

SELECT DISTINCT ReportTitle from HRPastReports WHERE [username] LIKE '%" + loggedIn + "%'";
SELECT DISTINCT ReportTitle from FinancePastReports WHERE [username] LIKE '%" + loggedIn+ "%'";

How can I combine both the SELECT statements in to a single one?

Thank you

Upvotes: 0

Views: 153

Answers (2)

rabudde
rabudde

Reputation: 7732

You can do this only by UNION because you're referencing two different tables

SELECT DISTINCT ReportTitle from HRPastReports WHERE [username] LIKE '%" + loggedIn + "%'";
UNION
SELECT DISTINCT ReportTitle from FinancePastReports WHERE [username] LIKE '%" + loggedIn+ "%'";

Upvotes: 2

Rashmi Kant Shrivastwa
Rashmi Kant Shrivastwa

Reputation: 1167

you can use UNION operator like

SELECT DISTINCT ReportTitle from HRPastReports WHERE [username] LIKE '%" + loggedIn + "%'";
union all
SELECT DISTINCT ReportTitle from FinancePastReports WHERE [username] LIKE '%" + loggedIn+ "%'";

Upvotes: 1

Related Questions