I wrote a query with a subquery and both of them have an order by. I have a weird behavior, sometimes I don't get back all the rows, sometimes I have 0 rows, but if I run the inner query I have the correct rows always. Is there some limitation about subquery? MySql is version 5.5
thanks a lot
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;
Why do you don't, on your sub-query, select traIn.date, and after that, select only fakeId, balance Somethings like that :
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.