Mysql 如何使此查询更易于阅读

Mysql 如何使此查询更易于阅读,mysql,Mysql,我在下面有一个大问题,我不喜欢的是: 我无法在查询中引用项目\u tax\u percent\u regular和项目\u tax\u percent\u cumulative作为别名。(每当我想使用它们的时候,我都要重复这个大总和语句) 它太大了 这个查询正是我想要的,但它太大了 CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_sales.deleted as deleted, date(sale_time) as

我在下面有一个大问题,我不喜欢的是:

  • 我无法在查询中引用项目\u tax\u percent\u regular和项目\u tax\u percent\u cumulative作为别名。(每当我想使用它们的时候,我都要重复这个大总和语句)
  • 它太大了
这个查询正是我想要的,但它太大了

CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_items.sale_id, comment,payment_type, customer_id, employee_id, phppos_items.item_id, NULL as item_kit_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, category, 

SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END) as item_tax_percent_regular, 
SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END) as item_tax_percent_cumulative, discount_percent, 
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, phppos_sales_items.line as line, serialnumber, phppos_sales_items.description as description, 
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100),2) as total, 

ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax, 

(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit FROM phppos_sales_items INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id 

LEFT OUTER JOIN phppos_suppliers ON phppos_items.supplier_id=phppos_suppliers.person_id 
LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" 
GROUP BY sale_id, item_id, line) 

UNION ALL

(SELECT phppos_sales.deleted as deleted, date(sale_time) as sale_date, phppos_sales_item_kits.sale_id, comment,payment_type, customer_id, employee_id, NULL as item_id, phppos_item_kits.item_kit_id, '' as supplier_id, quantity_purchased, item_kit_cost_price, item_kit_unit_price, category, 
SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END) as item_kit_tax_percent_regular, SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END) as item_kit_tax_percent_cumulative, discount_percent, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) as subtotal,
phppos_sales_item_kits.line as line, '' as serialnumber, phppos_sales_item_kits.description as description, ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)+ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100),2) as total,

 ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) +((ROUND((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100),2) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax, 

(item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) - (item_kit_cost_price*quantity_purchased) as profit FROM phppos_sales_item_kits 
INNER JOIN phppos_sales ON phppos_sales_item_kits.sale_id=phppos_sales.sale_id INNER JOIN phppos_item_kits ON phppos_sales_item_kits.item_kit_id=phppos_item_kits.item_kit_id 
LEFT OUTER JOIN phppos_sales_item_kits_taxes ON phppos_sales_item_kits.sale_id=phppos_sales_item_kits_taxes.sale_id and phppos_sales_item_kits.item_kit_id=phppos_sales_item_kits_taxes.item_kit_id and phppos_sales_item_kits.line=phppos_sales_item_kits_taxes.line WHERE date(sale_time) 
BETWEEN "2011-07-28" and "2011-07-28" 
GROUP BY sale_id, item_kit_id, line) ORDER BY sale_id, line
使用“as”创建别名:

...
LEFT OUTER JOIN phppos_sales_item_kits_taxes AS sikTaxes ON sikTaxes.sale_id=phppos_sales.sale_id
...
您可以在整个查询中使用您创建的别名


将一些算法拉入用户定义的函数也可能会有所帮助。

这怎么样?我已经加入了一些换行格式来辅助。为了便于注释,我将运算符放在下一行(例如逗号分隔或减法):

CREATE TEMPORARY TABLE phppos_sales_items_temp
(
SELECT phppos_sales.deleted as deleted
, date(sale_time) as sale_date
, phppos_sales_items.sale_id
, comment
, payment_type
, customer_id
, employee_id
, phppos_items.item_id
, NULL as item_kit_id
, supplier_id
, quantity_purchased
, item_cost_price
, item_unit_price
, category
, SUM(CASE
     WHEN cumulative != 1
     THEN percent
     ELSE 0
     END) as item_tax_percent_regular
, SUM(CASE
     WHEN cumulative = 1
     THEN percent
     ELSE 0
     END) as item_tax_percent_cumulative
, discount_percent
, (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100) as subtotal
, phppos_sales_items.line as line
, serialnumber
, phppos_sales_items.description as description
, ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
  + ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
  + (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + ((ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100))
  * (SUM(CASE
        WHEN cumulative = 1
        THEN percent
        ELSE 0
        END)) / 100), 2) as total
, ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + ((ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent/100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100))
  * (SUM(CASE
        WHEN cumulative = 1
        THEN percent
        ELSE 0
        END)) / 100) as tax
, (item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent / 100)
  - (item_cost_price * quantity_purchased) as profit
FROM phppos_sales_items
INNER JOIN phppos_sales ON phppos_sales_items.sale_id = phppos_sales.sale_id
INNER JOIN phppos_items ON phppos_sales_items.item_id = phppos_items.item_id 
LEFT OUTER JOIN phppos_suppliers
   ON phppos_items.supplier_id = phppos_suppliers.person_id 
LEFT OUTER JOIN phppos_sales_items_taxes
   ON phppos_sales_items.sale_id = phppos_sales_items_taxes.sale_id
   AND phppos_sales_items.item_id = phppos_sales_items_taxes.item_id
   AND phppos_sales_items.line = phppos_sales_items_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" 
GROUP BY sale_id
, item_id
, line
) 

UNION ALL

(
SELECT phppos_sales.deleted as deleted
, date(sale_time) as sale_date
, phppos_sales_item_kits.sale_id
, comment
, payment_type
, customer_id
, employee_id
, NULL as item_id
, phppos_item_kits.item_kit_id
, '' as supplier_id
, quantity_purchased
, item_kit_cost_price
, item_kit_unit_price
, category
, SUM(CASE
     WHEN cumulative != 1
     THEN percent
     ELSE 0
     END) as item_kit_tax_percent_regular
, SUM(CASE
     WHEN cumulative = 1
     THEN percent
     ELSE 0
     END) as item_kit_tax_percent_cumulative
, discount_percent
, (item_kit_unit_price * quantity_purchased-item_kit_unit_price * quantity_purchased * discount_percent / 100) as subtotal
, phppos_sales_item_kits.line as line
, '' as serialnumber
, phppos_sales_item_kits.description as description
, ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  + ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + ((ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100))
  * (SUM(CASE WHEN
        cumulative = 1
        THEN percent
        ELSE 0
        END)) / 100), 2) as total
, ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
     WHEN cumulative != 1
     THEN percent
     ELSE 0
     END) / 100), 2)
  + ((ROUND((item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  * (SUM(CASE
        WHEN cumulative != 1
        THEN percent
        ELSE 0
        END) / 100), 2)
  + (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100))
  * (SUM(CASE
        WHEN cumulative = 1
        THEN percent
        ELSE 0
        END)) / 100) as tax
, (item_kit_unit_price * quantity_purchased - item_kit_unit_price * quantity_purchased * discount_percent / 100)
  - (item_kit_cost_price * quantity_purchased) as profit
FROM phppos_sales_item_kits 
INNER JOIN phppos_sales
   ON phppos_sales_item_kits.sale_id = phppos_sales.sale_id
INNER JOIN phppos_item_kits
   ON phppos_sales_item_kits.item_kit_id = phppos_item_kits.item_kit_id 
LEFT OUTER JOIN phppos_sales_item_kits_taxes
   ON phppos_sales_item_kits.sale_id = phppos_sales_item_kits_taxes.sale_id
   AND phppos_sales_item_kits.item_kit_id = phppos_sales_item_kits_taxes.item_kit_id
   AND phppos_sales_item_kits.line = phppos_sales_item_kits_taxes.line
WHERE date(sale_time) BETWEEN "2011-07-28" and "2011-07-28" 
GROUP BY sale_id
, item_kit_id
, line
)
ORDER BY sale_id
, line

当然是换行和缩进。一个简单的解决方案是把SQL放到一个美化器中。e、 当我在多行中继续一个语句时,我总是喜欢在一行末尾加上一些不完整的内容,这样读者就会看到下一行。运算符或逗号就可以了,或者与下一行的目标进行“内部联接”。缩进可以实现这一点。注释列表中的最后一行将留下一个悬空的逗号,导致查询失败,因此我倾向于将它们放在前面。