开发者

Need help in building a MySQL query

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜