按供应商分组多行

时间:2014-07-10 06:00:40

标签: php mysql

我有一个查询从数据库中获取结果,如此屏幕截图所示,但我希望结果像第二个屏幕截图一样。

查询是:

SELECT ED.modelno,
       ED.grade,
       ED.type,
       ED.class,
       ED.thickness,
       ED.over_size_type ,
       ED.description,
       ED.thread_pitch,
       ED.thread_series,
       ED.head_mark,
       ED.qty_per_box,
       ED.no_boxes,
       ED.length,
       ED.qty,
       SQ.s_id,
       SQ.supplier_code,
       SQ.currancy,
       SQ.moq,
       SQ.weight_per1000,
       SQ.us_dollor_per1000,
       SQ.final_us_doller,
       SQ.discount_ontotal,
       SQ.us_dollor_kg,
       SQ.ttl_us_dollor,
       SQ.ttl_kg,
       SQ.evaluation,
       SQ.totadiscount_value,
       SQ.pallet,
       R.name AS nam,
       R.companyname
FROM enquiry_details AS ED
LEFT OUTER JOIN supplier_quotation AS SQ ON ED.enq_detail_id=SQ.enq_detail_id
LEFT OUTER JOIN registration AS R ON SQ.s_id=R.id
WHERE ED.enq_id=15
GROUP BY R.name

enter image description here

1 个答案:

答案 0 :(得分:0)

你能试试吗?将您的小组改为

Group by R.name, ED.modelno
With Rollup.

感谢。

这是我的测试结果。

enter image description here

嗨,这是查询。

我只是假设你需要总计 totadiscount_value

SELECT coalesce(ED.modelno,'Sub Total') modelno,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.grade) grade,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.type) type,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.class) class,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.thickness) thickness,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.over_size_type) over_size_type,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.description) description,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.thread_pitch) thread_pitch,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.thread_series) thread_series,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.head_mark) head_mark,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.qty_per_box) qty_per_box,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.no_boxes) no_boxes,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.length) length,
   if (coalesce(ED.modelno,'Total')='Total',null,ED.qty) qty,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.s_id) s_id,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.supplier_code) supplier_code,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.currancy) currancy,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.moq) moq,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.weight_per1000) weight_per1000,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.us_dollor_per1000) us_dollor_per1000,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.final_us_doller) final_us_doller,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.discount_ontotal) discount_ontotal,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.us_dollor_kg) us_dollor_kg,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.ttl_us_dollor) ttl_us_dollor,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.ttl_kg) ttl_kg,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.evaluation) evaluation,
   sum(SQ.totadiscount_value) totadiscount_value,
   if (coalesce(ED.modelno,'Total')='Total',null,SQ.pallet) pallet,
   coalesce(R.name,'Total') as nam,
   if (coalesce(ED.modelno,'Total')='Total',null,R.companyname) companyname
 FROM enquiry_details AS ED
  LEFT OUTER JOIN supplier_quotation AS SQ ON ED.enq_detail_id=SQ.enq_detail_id
  LEFT OUTER JOIN registration AS R ON SQ.s_id=R.id
 WHERE ED.enq_id=15
 group by R.name,ED.modelno
  with rollup

感谢。