gpt4 book ai didi

php - MYSQL中的if条件用于排序距离和检查状态

转载 作者:行者123 更新时间:2023-11-29 14:15:20 26 4
gpt4 key购买 nike

我正在编写一个代码来显示最近的商店位置及其营业状态(例如商店是否按月营业或关闭)。我正在使用以下代码,该代码对于距离来说效果很好,但我想对列表进行排序以显示数据及其打开状态,然后根据距离对列表进行排序。你能帮我把我的PHP放在下面的if条件代码中查询吗我有 2 列 'start_date' ,值 'int' 作为月份,'end_date' ...我使用以下 php 代码来显示html 中的状态也是如此。您能否帮助我改进我的查询,首先按打开状态对结果进行排序,然后按距离进行排序

$currMonth = date('m');

$query="SELECT *,
6371 * 2 * ASIN(SQRT( POWER(SIN(($lat - latitude) * pi()/180 / 2), 2) + COS($lat * pi()/180) * COS(latitude * pi()/180) *
POWER(SIN(($long - longitude) * pi()/180 / 2), 2) )) as
distance, $currMonth >= start_date as open FROM table_name
GROUP BY Id HAVING distance <= $km ORDER by distance ASC ,open ASC";


if (($currMonth >= $row['start_date']) && ($currMonth <= $row['end_date']))
$Status = "open";
else
$Status = "close";

最佳答案

替换您的查询的这一行

$currMonth >= start_date as open FROM table_name

这样:

IF($currMonth >= start_date AND $currMonth <= end_date, 'open', 'closed') as open FROM table_name

它使用像这样的内联 IF 条件 - IF(condition, true_result, false_result)

如果条件为 true,则结果为 true_result。如果条件为 false,则结果为 false_result

如果您想先按开放状态排序,然后按距离排序,则还需要更改 ORDER 子句的顺序:

... ORDER BY open ASC, distance ASC

关于php - MYSQL中的if条件用于排序距离和检查状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com