简体   繁体   English

在请求 SQL 结果后,如何对它们进行排序?

[英]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?有没有办法在交付后对我的 SQL 结果进行排序? 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.我的代码目前返回结果,可以按日期排序ORDER BY date ASC感兴趣的数据库条目是。

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

Weeks represents how many weeks away the next date is. Weeks 表示离下一个日期还有多少周。 so 2020-07-21, 2020-08-03, and so on (I am able to return these values).所以 2020-07-21、2020-08-03 等等(我可以返回这些值)。 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. order by需要能够对日期进行排序并考虑从最近的一周到最远的一周的值。

I have tried just updating the database with a new field called final_date, although this makes everything cluttery.我曾尝试使用名为 final_date 的新字段更新数据库,尽管这会使一切变得混乱。

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. order by 需要能够对日期进行排序并考虑从最近的一周到最远的一周的值。

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;请注意,这假定列datedate数据类型,而不是字符串; else, you need to convert it to a date first, using str_to_date() for example.否则,您需要先将其转换为日期,例如使用str_to_date()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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