有两个表:
产品
ID
(主键),
ProductName
按月计划
ProductID
(主键)(多对一链接到 Products
表),
MonthNumber
(主键),
生产数量
如何编写 SELECT 语句来检索以下格式的结果?
ProductName、QtytoProduceMonth1、QtytoProduceMonth2、QtytoProduceMonth3、QtytoProduceMonth4、QtytoProduceMonth5、QtytoProduceMonth6、QtytoProduceMonth7、QtytoProduceMonth8、QtytoProduceMonth9、QtytoProduceMonth10、QtytoProduceMonth11、QtytoProduceMonth12
最佳答案
我不知道在 MySQL 中是否有更简单的方法,但这应该可行:
select
(select descriptions from products d where d.productid = p.productid )
description,
max(if(month=1,Qty, null)) m1,
max(if(month=2, Qty,null)) m2,
max(if(month=3, Qty, null)) m3
/* more here */
from Planned p
group by productid ;
关于mysql - 一行中一对多所有详细信息的 SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2305146/