'How to sort by date in mysql

I need to perform the following query in mysql.

SELECT 
    evaluationpart.id,
    evaluationpart.creation,
    evaluationpart.evaluationid,
    evaluationpart.partid,
    evaluation.horimeter,
    personcompressorpart.hourcapacity,
    evaluation.evaluationdate AS changedate,
    evaluation.averageworkload,
    @ed := DATEDIFF(curdate(), evaluation.evaluationdate) AS elapseddays,
    @uh:= @ed * evaluation.averageworkload AS usedhours,
    @htu:= personcompressorpart.hourcapacity - @uh AS hourstouse,
    @nc:= curdate() + INTERVAL (@htu/evaluation.averageworkload) DAY AS nextchange
FROM evaluationpart
LEFT JOIN evaluation ON evaluation.id = evaluationpart.evaluationid
LEFT JOIN personcompressorpart ON personcompressorpart.id = evaluationpart.partid
ORDER BY @nc ASC

But the Order By is not working and I'm getting this result enter image description here

Could anyone tell me why?



Solution 1:[1]

It seems that you are not using the column name in the ORDER BY clause. If you want to order the query result by the column named 'nextchange', the ORDER BY clause should be ORDER BY nextchange ASC. Here's the MySQL documentation on Sorting Rows: https://dev.mysql.com/doc/refman/8.0/en/sorting-rows.html I hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael Silva