SQL CASE和WHER并加入

时间:2015-10-14 06:04:38

标签: mysql

我写了一个像这样的SQL

"SELECT (
    CASE WHEN (1.00 - sys_pay.org_rate)*trans.txn_amt as agent_pay_current 
    < (trans.txn_amt - sys_pay.org_rate_limit) as agent_pay_max 
    THEN agent_pay_max 
    ELSE agent_pay_current END),
    FROM sys_org 
    INNER JOIN trans ON sys_org.mid = trans.mid 
    INNER JOIN sys_pay ON sys_pay.mid = sys_org.mid 
    ORDER BY txn_date DESC limit 10"

它说我的SQL语法有错误,如何使其正确?

2 个答案:

答案 0 :(得分:0)

ms sql-server limit就是那样

"SELECT TOP 10 (
    CASE WHEN (1.00 - sys_pay.org_rate)*trans.txn_amt as agent_pay_current 
    < (trans.txn_amt - sys_pay.org_rate_limit) as agent_pay_max 
    THEN agent_pay_max 
    ELSE agent_pay_current END)
    FROM sys_org 
    INNER JOIN trans ON sys_org.mid = trans.mid 
    INNER JOIN sys_pay ON sys_pay.mid = sys_org.mid 
    ORDER BY txn_date DESC"

删除之前的逗号。感谢@rajeshmpanchal

没有发现另一个问题。

答案 1 :(得分:0)

试试这个,

"SELECT (
           CASE 
                WHEN ((1.00 - sys_pay.org_rate) * trans.txn_amt) < (trans.txn_amt - sys_pay.org_rate_limit) THEN 
                     agent_pay_max
                ELSE agent_pay_current
           END
       )
FROM   sys_org
       INNER JOIN trans
            ON  sys_org.mid = trans.mid
       INNER JOIN sys_pay
            ON  sys_pay.mid = sys_org.mid
ORDER BY
       txn_date DESC"

"SELECT (
           CASE 
                WHEN ((1.00 - sys_pay.org_rate) * trans.txn_amt) < (trans.txn_amt - sys_pay.org_rate_limit) THEN (trans.txn_amt - sys_pay.org_rate_limit)
                ELSE ((1.00 - sys_pay.org_rate) * trans.txn_amt)
           END
       )            agent_pay_return
FROM   sys_org
       INNER JOIN trans
            ON  sys_org.mid = trans.mid
       INNER JOIN sys_pay
            ON  sys_pay.mid = sys_org.mid
ORDER BY
       txn_date     DESC"

agent_pay_return将是根据案例检查的返回值。