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
Thanks in advance.
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%
I only want it to return values that also exist in table2.Thanks in advance.
If you are only wanting to return records from Table1 that are also in Table2, then this should work below.
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
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%'
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
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
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
I want to include only rows that contain "msiexec"
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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