我已經有這個疑問:使用如果在一個MySQL查詢
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)
)
觀測數據: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))
什麼是錯誤陳述? –