我用子查询编写了一个查询,它们都有一个顺序。 我有一个奇怪的行为,有时我没有回到所有行,有时我有0行,但如果我运行内部查询我总是有正确的行。子查询有一些限制吗? MySql是版本5.5
非常感谢
SELECT
*
FROM
(
SELECT
@fakeId := @fakeId + 1 AS fakeId,
@balance := (@balance + traIn.amount) AS balance,
FROM
(SELECT @fakeId := 0, @balance := 0) AS vars,
table1 traIn
INNER JOIN table2 traTypeIn ON traIn._id = traTypeIn.id
INNER JOIN table3 ptfIn ON traIn.ptf_id = ptfIn.id
LEFT JOIN `user` u ON u.id = traIn.user_create_id
LEFT JOIN `user` up ON up.id = traIn.user_update_id
WHERE
<--- a couple of constraints -->
ORDER BY
traIn.date
) AS data
ORDER BY data.fakeId DESC
LIMIT 50;
答案 0 :(得分:0)
为什么不在子查询中选择traIn.date,之后只选择fakeId,balance 这样的事情:
SELECT
data.fakeId, data.balance
FROM
(
SELECT
@fakeId := @fakeId + 1 AS fakeId,
@balance := (@balance + traIn.amount) AS balance,
traIn.date as date1
FROM
(SELECT @fakeId := 0, @balance := 0) AS vars,
table1 traIn
INNER JOIN table2 traTypeIn ON traIn._id = traTypeIn.id
INNER JOIN table3 ptfIn ON traIn.ptf_id = ptfIn.id
LEFT JOIN `user` u ON u.id = traIn.user_create_id
LEFT JOIN `user` up ON up.id = traIn.user_update_id
WHERE
<--- a couple of constraints -->
) AS data
ORDER BY data.date1, data.fakeId DESC
LIMIT 50;