开发者

Need help in building a MySQL query

开发者 https://www.devze.com 2023-03-03 00:07 出处:网络
I have one table that contains two columns: item ID item Name I have another products table that has three columns:

I have one table that contains two columns:

  • item ID
  • item Name

I have another products table that has three columns:

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消