I have one table that contains two columns:
- item ID
- item Name
I have another products table that has three columns:
- product name
- item id开发者_StackOverflow 1
- item id 2
I want to build a query that will show this:
- product name
- item name for item id 1
- item name for item id 2
How do I do this?
select p.name, i1.name, i2.name from products p
join items i1 on i1.id=p.item1_id
join items i2 on i2.id=p.item2_id
where p.id=?;
replace ? with your parameter. enjoy,
SELECT ProductName, a1.ItemName AS Item1, a2.ItemName AS Item2
FROM Products
INNER JOIN Items a1 ON Products.ItemsID1 = a1.ItemID
INNER JOIN Items a2 ON Products.ItemsID2 = a2.ItemID
精彩评论