我有一个奇怪的问题,那就是当我第一次运行sql命令时,它显示错误的结果,但是当我第二次运行相同的代码时,结果是正确的。 错误的结果是:
预期结果是:
sql命令是:
SELECT srNumber, end_on, start_on, teamEntityId
FROM (
SELECT srNumber, end_on, start_on, teamEntityId,
@lastrow := IF(@sr_Number = srNumber, @lastrow + 1,
IF(@sr_Number := srNumber, 1, 0)) AS lastrow
FROM assign
CROSS JOIN (SELECT @lastrow := 0, @sr_Number = '') AS vars
ORDER By srNumber, assignId DESC) AS t
WHERE t.lastrow = 1 AND t.teamEntityId = '00000168752ac161-02420aff00230001'
答案 0 :(得分:0)
在更新的MySQL版本中,您需要使用变量在之前排序数据。因此,替换:
FROM assign
具有:
FROM (SELECT a.* FROM assign a ORDER BY srNumber, assignId DESC) a