我需要运行查询以从数据库中获取一些数据。事情是我使用的查询有效,但是需要很长时间。
SELECT SHH1.CUST_NO,
SHH1.CUST_NAME,
ADDR.BVADDREMAIL
FROM SALES_HISTORY_HEADER SHH1
INNER JOIN ADDRESS ADDR ON (SHH1.CUST_NO=ADDR.CEV_NO)
INNER JOIN CUSTOMER CUST ON (SHH1.CUST_NO=CUST.CUS_NO)
WHERE CUST.HOLD = 0
AND SHH1.CUST_NO IN (SELECT SHH2.CUST_NO
FROM SALES_HISTORY_HEADER SHH2
GROUP BY SHH2.CUST_NO
HAVING Max(SHH2.IN_DATE) < '20120101')
GROUP BY SHH1.CUST_NO,
SHH1.CUST_NAME,
ADDR.BVADDREMAIL
我不是很擅长此事,所以想知道你们中的任何一个可以帮助我吗?谢谢。
最佳答案
我认为您不需要子选择。以下应该给您相同的结果:
SELECT SHH1.CUST_NO,SHH1.CUST_NAME,ADDR.BVADDREMAIL
FROM SALES_HISTORY_HEADER SHH1
INNER JOIN ADDRESS ADDR ON (SHH1.CUST_NO=ADDR.CEV_NO)
INNER JOIN CUSTOMER CUST ON (SHH1.CUST_NO=CUST.CUS_NO)
WHERE CUST.HOLD = 0
GROUP BY SHH1.CUST_NO,SHH1.CUST_NAME,ADDR.BVADDREMAIL
HAVING Max(SHH1.IN_DATE) < '20120101'
关于mysql - 优化一个SQL查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15277554/