我目前使用的数据库有不同的条目,如日期、姓名,但也有一个列的时间“范围”。这基本上意味着在这个单元格中可以有一个像"10“这样的确定数字,也可以有一个像"10-15”或"5-10“这样的值。因此,我想在这里做的是按“平均值”((Lowest+Highest)/2)对它们进行排序。因此,在提到的3个值应该是5-10 10 10-15的情况下,我想知道是否可以以某种方式将其嵌入到SQL语句中。如果不可能,我想知道实现它的最简单的方法。
现在我正在通过几个条件把$SQL_statement放在一起,然后把所有东西放到$resultset中,然后和"while“一起使用。以下是一些代码片段:
$resultset=mysql_query($SQL_statement);
while ($currententry=mysql_fetch_array($resultset))
{
echo $currententry['Platform'];
echo $currententry['PlaytimeInH']."h";
}发布于 2017-01-22 06:17:43
您可以使用substring_index()和算术来完成此操作:
order by (substring_index(col, '-', 1) + 0 +
substring_index(col, '-', -1) + 0
) / 2除以2是不必要的,但您需要在问题中指定平均值。
请注意,即使col中没有连字符,上面的代码也会起作用。
发布于 2017-01-22 04:43:30
您可以将select与order by中的case when子句一起使用
select col1, col2, cole
from your_table
order by case
when your_column = '5-10' then 1
when your_column = '10-15' then 2
when your_column = '15-20' then 3
else 4
end https://stackoverflow.com/questions/41784097
复制相似问题