按顺序选择变量mysql

时间:2014-02-15 19:48:57

标签: mysql stored-procedures

我有以下sql查询:

Select
sum(
    (IF(notification_type = 'qAccepted',credit*0.5,0)) + 
    (IF(notification_type = 'creditScored',credit,0)) +
    (IF(notification_type = 'flagPositive',credit,0))
) into b_credit
from notifications n
left join user_answers ua on ua.question_id = n.question_id and ua.user_id = n.user_id 
left join user_credits uc on uc.user_id = n.user_id
where (unix_timestamp() - timestamp) < 7*86400  
group by n.user_id  
order by b_credit desc 
limit 1; 

如何通过b_credit变量订购?

修改 这使b_credit变量为NULL。程序运行正常。

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT MAX(x_credit) INTO b_credit
FROM (
    Select
    sum(
        (IF(notification_type = 'qAccepted',credit*0.5,0)) + 
        (IF(notification_type = 'creditScored',credit,0)) +
        (IF(notification_type = 'flagPositive',credit,0))
    ) AS x_credit
    from notifications n
    left join user_answers ua on ua.question_id = n.question_id and ua.user_id = n.user_id 
    left join user_credits uc on uc.user_id = n.user_id
    where (unix_timestamp() - timestamp) < 7*86400
    group by n.user_id
) x