SELECT mob_no,
(SELECT max(str_to_date(debit_date,'%d/%m/%Y'))
FROM client_debit_bal
WHERE mob_no='".$mobno."') AS date,
sum(debit_amt) AS debit
FROM client_debit_bal
WHERE date_format(str_to_date(debit_date,'%d/%m/%Y'), '%Y-%m-%d')= date
实际上这里date
是一个别名变量,我无法用于条件。请帮我解决这个问题。
示例数据
-----------------------------------
date | cre_amount | mob_no
------------------------------------
23/01/2013 | 100 | 123456
------------------------------------
24/01/2013 | 500 | 123456
------------------------------------
25/01/2013 | 500 | 123456
------------------------------------
26/01/2013 | 900 | 123456
------------------------------------
29/01/2013 | 1000 | 123456
------------------------------------
29/01/2013 | 1300 | 123456
------------------------------------
29/01/2013 | 1200 | 123456
------------------------------------
预期输出为max(date)
和sum(cre_amount)
,结果为3500
(表示选择最大date
并从最长日期返回cre_amount
的总和)
答案 0 :(得分:1)
简单地使用
select DATE_FORMAT(max(cast(str_to_date(`date`, '%d/%m/%Y') as date)),'%d/%m/%Y'),
sum(cre_amount) from table1 where date=(select DATE_FORMAT(max(cast(str_to_date(`date`, '%d/%m/%Y') as date)),'%d/%m/%Y')
from table1)