我正在尝试添加一个新的列,它按日期显示行结果的排名(或序列)。
我写过:
SELECT
@row_number:=(CASE
WHEN @member_id = lh.member_id and lc.ladder_advocacy is not null
THEN @row_number + 1
when @member_id = lh.member_id and lc.ladder_advocacy is null then "null"
ELSE 1 /* there is an error here - i need it to return a 1 if not null, then 2 for the 2nd instance, etc */
END) AS rank_advocacy,
@member_id:=lh.member_id AS member_id,
lh.ladder_change,
lc.name,
lc.ladder_advocacy,
lc.ladder_elected,
lc.ladder_policy,
lc.ladder_engagement,
lc.ladder_newventure,
lc.ladder_collective,
lc.is_trigger
FROM
leenk_ladder_history AS lh
LEFT JOIN
leeds_so.leenk_ladder_config AS lc ON lh.ladder_config_id = lc.id
WHERE
ladder_change = 1 AND trigger_active = 1
ORDER BY member_id, trigger_event_date DESC;
第4行有一个错误,我不知道如何修复它。对于第一个结果,我想返回1。对于第二个结果,我想返回@row\u number+1。第三个结果,@row\u number+2(etc)。
我如何做到这一点?
1条答案
按热度按时间np8igboo1#
我不明白情况如何
lc.ladder_advocacy is not null
正在使用。但是,基本结构是:一些非常重要的观点:
你需要分配
@member_id
以及@row_number
用同样的表达。mysql(与所有其他数据库一样)不保证表达式的求值顺序。在mysql的最新版本中,我认为
ORDER BY
需要进入子查询,外部查询中包含变量表达式。