我已经有这个查询:
cursor.execute("SELECT calldate, dst, billsec, accountcode, disposition,
(billsec/60*%s) as total
FROM cdr
WHERE calldate >= '%s' and calldate < '%s'
and disposition like '%s' and accountcode = '%s'
and dst like '%s'" %(rate, start_date, end_date, status, accountcode, destino)
)
Obs: dst 是一个电话号码。
但现在我需要做的是:如果 dst 的第 5 个字符 <= 5,则 billsec/60*0.11 总计,否则 billsec/60*0.16。
可能吗?
这样我在mysql语法中出现错误:
cursor.execute("""SELECT calldate, dst, billsec, accountcode, disposition,
case when cast(substring(dst,4,1), unsigned) <= 5 then
billsec/60*%s as total
else
billsec/60*%s as total
end case
FROM cdr where calldate >= '%s' and calldate < '%s' and disposition like '%s' and accountcode = '%s' and dst like '%s'""" %(rate_fixo, rate_movel, start_date, end_date, status, accountcode, destino))