【发布时间】:2015-08-06 09:38:56
【问题描述】:
如何优化以下查询。尽管这解决了问题,但请有人帮我优化查询。
select invoice_tbl.invoice_no, invoice_tbl.invoice_date , adddate(invoice_date , interval remdays day) as remainder_date , remainder_tbl.desc
from ( select max(id) as id,invoice_no
from invoice_tbl inv
inner join remainder_tbl where adddate(invoice_date , interval remdays day) <= now() group by invoice_no ) as remdetails
inner join invoice_tbl on invoice_tbl.invoice_no = remdetails.invoice_no
inner join remainder_tbl on remainder_tbl.id = remdetails.id
我们有 2 个表格,invoice_tbl 和 remainder_tbl。
invoice_tbl
-----------
*--------------------------------------------*
| invoice_no | invoice_date | patient_name |
|-------------|---------------|--------------|
| 1 | 2015-07-15 | xxxxxxxxxxxx |
| 2 | 2015-07-29 | aaaaaaaaaaaa |
*-------------*---------------*--------------*
remainder_tbl
-------------
*----------------------------------------*
| id | remdays | desc |
|-----|----------|-----------------------|
| 1 | 5 | First Remainder |
| 2 | 8 | Second Remainder |
| 3 | 10 | Third Remainder |
| 4 | 15 | Fourth Remainder |
*-----*----------*-----------------------*
当我运行查询时,它应该显示为
*------------------------------------------------------*
| invoice_no | invoice_date | remainder_date | desc |
|------------|--------------|----------------|---------|
| 1 | 2015-07-15 | 2015-07-30 | Fourth |
| 2 | 2015-07-29 | 2015-08-06 | Second |
*------------*--------------*----------------*---------*
【问题讨论】: