简体   繁体   English

结合两个具有不同功能的SELECT查询

[英]Combine two SELECT queries with different function

I have one table for reports. 我有一张桌子供报告。 My first select query is that combines all sales tables 我的首选查询是合并所有销售表

SELECT *, ds_sales.id_sales
FROM ds_sales LEFT JOIN
     ds_payment 
     ON ds_sales.id_sales=ds_payment.id_sales LEFT JOIN 
     customer_info
     ON ds_payment.id_customer=customer_info.id_customer INNER JOIN
     ds_salesdetails
     ON ds_salesdetails.id_sales=ds_sales.id_sales
WHERE customer_info.id_customer = '".$_POST["id_customer"]."'

This is the result of the query 这是查询的结果

这是查询的结果

and my second query is this, to filter the same sales serial number, since the ds_salesdetails table have many sales serial number 我的第二个查询是这个,以过滤相同的销售序列号,因为ds_salesdetails表具有许多销售序列号

select ds_salesdetails.id_sales, count(*),
       group_concat(ds_salesdetails.id_product)
from ds_salesdetails
having count(*) >= 1

I need to merge them to create a report for customers sales. 我需要合并它们以创建客户销售报告。

Join with the grouped subquery instead of the whole table. 与分组的子查询而不是整个表联接。

SELECT *, ds_sales.id_sales
FROM ds_sales LEFT JOIN
     ds_payment 
     ON ds_sales.id_sales=ds_payment.id_sales LEFT JOIN 
     customer_info
     ON ds_payment.id_customer=customer_info.id_customer LEFT JOIN
    (SELECT id_sales, count(*) as product_count,
           group_concat(ds_salesdetails.id_product) AS product_list
    from ds_salesdetails
    GROUP BY id_sales) AS grouped ON grouped.id_sales = ds_sales.id_sales
WHERE customer_info.id_customer = '".$_POST["id_customer"]."'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM