我有以下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。程序运行正常。
答案 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