Link to home
Start Free TrialLog in
Avatar of crash1624
crash1624

asked on

SQL query multiple tables

Hello,

I need to run a query to select any values that exist in table1, only if they exist in table2 as well.  For example if I ran this query
Select Name From Table1 Where ComputerID=%computerid%

Open in new window

I only want it to return values that also exist in table2.

Thanks in advance.
Avatar of ralmada
ralmada
Flag of Canada image

Something like this
Select a.Name From Table1 a Inner join Table2  b on a.computerid = b.computerid Where a.ComputerID=%computerid%

Assuming both tables have computerid in common
If you are only wanting to return records from Table1 that are also in Table2, then this should work below.

SELECT t1.name
FROM Table1 t1
INNER JOIN Table2 t2
  ON t1.computerid = t2.computerid

Open in new window


I'm not sure what you're doing with the %computerid% piece of your code, if that's to represent the joining of the computer ID's, then you don't need to include that in your statement.

However, if you are searching for a specific computer ID within your Table1 table, then you can do this below.

SELECT t1.name
FROM Table1 t1
INNER JOIN Table2 t2
  ON t1.computerid = t2.computerid
WHERE t1.computerid LIKE '%computerid%'

Open in new window

Of course, your where clause has a syntax error there

set @computerid = 'somecomputer'

Select a.Name From Table1 a
Inner join Table2  b on a.computerid = b.computerid
Where a.ComputerID like '%' + @computerid + '%'

or maybe just

Select a.Name From Table1 a
Inner join Table2  b on a.computerid = b.computerid
Where a.ComputerID = @computerid
Avatar of crash1624
crash1624

ASKER

Thanks for the answers, I'm also trying to exclude certain rows

SELECT a.Name FROM software a INNER JOIN applicationblacklist  b ON a.name = b.name 

Open in new window

I want to include only rows that contain "msiexec"
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial