简体   繁体   中英

How can I order SQL results after they have been requested?

Is there a way to order my SQL results after they have been delivered? So far I have two values. My code currently returns the results and can sort by the dates ORDER BY date ASC The database entries of interest are.

| date       | weeks |
| 2020-07-01 | 2     | 
| 2020-07-01 | 3     |
| 2020-07-01 | 5     |

Weeks represents how many weeks away the next date is. so 2020-07-21, 2020-08-03, and so on (I am able to return these values). The order by needs to be able to order the dates and take into account the week value from the closest week to the furthest.

I have tried just updating the database with a new field called final_date, although this makes everything cluttery.

When the data is returned, I can find the new date using:

$addWeeks = strtotime('+'.$numberOfWeeks.' weeks', strtotime($last));

Although, I cannot find a way to order the results by that new value.

The order by needs to be able to order the dates and take into account the week value from the closest week to the furthest.

I think you want:

select t.*
from mytable t
order by t.date + interval t.weeks week

Note that this assumes that column date is of date datatype, not string; else, you need to convert it to a date first, using str_to_date() for example.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM