user3501263
user3501263

Reputation: 1

Combining 2 tables

Wondering if someone could help me combine (in SQL) two tables into one query. I have tried the UNION command to combine the tables, but get the error that the number of columns in the two selected tables or queries of a union query do not match. Here is my SQL code:

SELECT [CustID], [First], [Last]
FROM Customers
WHERE [First] IN ("Angel", "Mike", "Phan")
UNION 
SELECT [PartID]
FROM Sales
WHERE [PartID] IN ("Y450T", "Y430P", "G814T"); 

Upvotes: 0

Views: 49

Answers (1)

rory.ap
rory.ap

Reputation: 35260

I'm thinking what you mean to do is a JOIN based on evidence from your question. If there is some column that is common between CUSTOMERS and SALES -- say [CustID] -- then you can join the two tables.

Something like this?

SELECT a.[CustID], [First], [Last], b.[PartID]
FROM Customers a
INNER JOIN Sales b
ON a.[CustID]=b.[CustID]
WHERE [First] IN ("Angel", "Mike", "Phan")
AND [PartID] IN ("Y450T", "Y430P", "G814T")

Upvotes: 1

Related Questions