表结构......
计划大师
book receipt coupon created
100 700 1200 12-03-2015
100 700 1201 12-03-2015
100 700 1202 12-03-2015
100 701 1203 12-03-2015
100 701 1204 12-03-2015
100 701 1205 12-03-2015
图书问题表
book_no Alloted_city issue_date
100 nagpur 15-03-2015
100 nagpur 18-03-2015
Book_return表
book surveyor used_receipt city
1000 raj 700 nagpur
以下是我的预期输出..
book printeddate issuedate balancereceipt allcoupon city
100 12-03-2015 15-03-2015 701 1203,1204,1205 nagpur
以下是我的SQL查询。
这很好用..但需要4到5分钟才能执行50到80行... plz帮我优化以下查询......
SELECT DISTINCT BI.book_no AS book,BI.issue_date As Issuedate,BI.Alloted_city As city,SM.created AS PrintedDate,
SM.receipt_no as AllReceipt,GROUP_CONCAT(DISTINCT SM.coupon ORDER BY coupon ASC) As Allcoupon FROM book_issue BI LEFT JOIN scheme_master SM ON SM.book_no2=BI.book_no WHERE SM.receipt_no NOT IN (select used_receipt from book_return) AND BI.Alloted_city = :cityname AND BI.book_no BETWEEN :book_no AND :book2 GROUP BY SM.receipt_no order by SM.receipt_no ASC
答案 0 :(得分:0)
像这样使用,在非条件中添加where子句,
SELECT DISTINCT BI.book_no AS book,BI.issue_date As Issuedate,BI.Alloted_city As city,SM.created AS PrintedDate,
SM.receipt_no as AllReceipt,GROUP_CONCAT(DISTINCT SM.coupon ORDER BY coupon ASC) As Allcoupon
FROM book_issue BI
LEFT JOIN scheme_master SM ON SM.book_no2=BI.book_no
WHERE SM.receipt_no NOT IN (select used_receipt from book_return where used_receipt = SM.receipt_no)
AND BI.Alloted_city = :cityname AND BI.book_no BETWEEN :book_no AND :book2
GROUP BY SM.receipt_no order by SM.receipt_no ASC