使查询更简单

时间:2019-04-15 11:26:01

标签: php mysql sql

我有此查询将连接到我的数据库。 我遇到了加载所需数据的速度太慢的问题。

SELECT
IFNULL((SELECT SUM(c.due_to_office) AS dt_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'DT'),0) AS dt_amounts,
IFNULL((SELECT SUM(c.due_to_office) AS rt_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'RT'),0) AS rt_amounts,
IFNULL((SELECT SUM(c.due_to_office) AS it_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'IT'),0) AS it_amounts,
IFNULL((SELECT SUM(c.due_to_office) AS lr_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'LR'),0) AS lr_amounts,
IFNULL((SELECT SUM(c.due_to_office) AS ir_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'IR'),0) AS ir_amounts,
IFNULL((SELECT SUM(c.due_to_office) AS v_amount FROM sales_entry_summary a, sales_products b, sales_entry c WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id AND b.so_category = 'V'),0) AS v_amounts;

2 个答案:

答案 0 :(得分:2)

从不FROM子句中使用逗号。 始终使用正确的,明确的,标准 JOIN语法。 JOIN成为SQL已有20多年了。现在该正确学习语言了。

我怀疑条件聚合会帮助您解决问题:

SELECT SUM(CASE WHEN p.so_category = 'DT' THEN c.due_to_office ELSE 0 END) AS dt_amount,
       SUM(CASE WHEN p.so_category = 'RT' THEN c.due_to_office ELSE 0 END) AS rt_amount,
       SUM(CASE WHEN p.so_category = 'IT' THEN c.due_to_office ELSE 0 END) AS it_amount,
       SUM(CASE WHEN p.so_category = 'LR' THEN c.due_to_office ELSE 0 END) AS ir_amount,
       SUM(CASE WHEN p.so_category = 'IR' THEN c.due_to_office ELSE 0 END) AS ir_amount,
       SUM(CASE WHEN p.so_category = 'V' THEN c.due_to_office ELSE 0 END) AS v_amount  
FROM sales_entry_summary ses JOIN
     sales_entry se
     ON ses.ses_id = se.ses_id JOIN
     sales_products sp
     ON ses.product_id = p.product_id
WHERE ses.sales_date = '2019-01-01' AND
      ses.status = 'AUDITED';

要获得更高的性能,您需要在sales_entry_summary(sales_date, status)上建立索引。

答案 1 :(得分:0)

SELECT b.so_category, SUM(c.due_to_office) AS dt_amount 
FROM sales_entry_summary a, sales_products b, sales_entry c 
WHERE a.ses_id = c.ses_id AND a.sales_date = '2019-01-01' AND a.`status` = 'AUDITED' AND a.product_id = b.product_id
GROUP BY b.so_category
HAVING b.so_category in ('DT', 'RT', 'IT', 'LR', 'IR', 'V')

将为您生成带有so_category和总和的行。如果您只想排成一行,那么请在上面的选择周围加上一个选择,并在以下情况下用例。

编辑

您当然可以加入表,这将是一个改进。