如何使用任何類型的連接或子查詢或嵌套查詢來減少此查詢運行時間。如何使用mysql減少嵌套查詢時間
select
`p`.`id`,
`p`.`name`,
SUM(s.quantity) as inst,
from `product` as `p`
LEFT JOIN sales as s ON s.pid=p.`id` AND s.sales_id IN (SELECT invoice.invoice_id FROM invoice WHERE invoice.ccid NOT IN (SELECT ccid FROM ticket WHERE st='1'))
where `p`.`hc` = '1'
GROUP BY `p`.`id`
order by `p`.`name` DESC
使用,而不是內部聯接的查詢和左的連接,而不是不.....這將減少查詢時間,因爲在查詢需要太多時間 –
'從產品選擇 p.id, p.name AS名稱, SUM(ss.instock)作爲研究所 爲p LEFT JOIN(SELECT s.id,s.pid,CASE COUNT(crt.salvage_part) WHEN 1 THEN 0 ELSE s.quantity END AS instock FR OM sales as s LEFT JOIN invoice as i ON i.invoice_id = s.sales_id LEFT JOIN crt ON crt.cc_id = i.cc_id AND crt.st ='1' GROUP BY s.id)as ss ON ss。 pid = p.id 其中p.hc ='1' GROUP BY p.id order by p.name DESC' **試過這個太,但不能減少它的運行時間。** –
這個問題是無意義的顯然,這個查詢會產生一個語法錯誤。 – Strawberry