[英]Know lower value and its column name from select
I have a PHP document with MySQL database storing with the following structure: 我有一个包含MySQL数据库的PHP文档,其结构如下:
DATE
FIELD DATE
字段 float
values corresponding to the 24 hours. float
值。 (The name is 1h, 2h, 3h, 4h... 24h) I fetch a row with the simple select: SELECT * from table where DATE = "2014-02-02"
obtaining the 25 fields. 我使用简单的选择获取一行:
SELECT * from table where DATE = "2014-02-02"
获得25个字段。
I want to know wich field of 1h, 2h, 3h.... has the lower value. 我想知道1h,2h,3h ...的字段值较低。 I'm trying doing that with PHP and min() function, but I only retreive the value, not the index of the array.
我正在尝试使用PHP和min()函数来执行此操作,但是我只能获取值,而不是数组的索引。
Is this possible a simple way to know this result? 这可能是了解此结果的简单方法吗? Maybe in the SQL function?
也许在SQL功能?
Thank you 谢谢
EDIT: I tried this (Considering $arraySQL has a valid result obtained before): 编辑:我尝试了此(考虑$ arraySQL之前获得有效的结果):
function ObtainArrayFromSQL($arraySQL){
$array = array();
for ($i = 1; $i < 25; $i++)
{
array_push($array, $data_day[$i."h"]);
}
return $array;
}
[...]
$array = ObtainArrayFromSQL($arraySQL)
echo min($array);
Assuming you are using MySQL, you can use the least()
function: 假设您正在使用MySQL,则可以使用
least()
函数:
select t.*,
(case when col1 = leastval then 'col1'
when col2 = leastval then 'col2'
when col3 = leastval then 'col3'
. . .
when col24 = leastval then 'col24'
end) as leastvalcol
from (select t.*,
least(col1, col2, . . . , col24) as leastval
from table t
) t;
Before complaining about all the typing you need, you should know that this query is much more complicated than it needs to be -- because you are storing things on a row that should be in a column. 在抱怨所有需要的输入之前,您应该知道此查询比需要的要复杂得多-因为您将事物存储在应该在列中的行中。 Your table should have a separate row for each hourly value, rather than putting all the daily values on one row.
您的表格中每个小时值应有单独的一行,而不是将所有每日值都放在一行中。 With a more normalized structure, your query would be much simpler.
使用更规范的结构,您的查询将更加简单。
EDIT: 编辑:
This table would have columns such as: 该表将包含诸如以下的列:
Date date,
Hour time, -- or int or varchar() depending on how you really want it captured
Value float
Then the query might look like: 然后查询可能看起来像:
select date, min(value) as minvalue,
substring_index(group_concat(hour order by value asc), ',', 1) as minhour
from newtable t
group by date;
At first, thank you everyone for the help. 首先,感谢大家的帮助。
Finally I made the solution with PHP instead of manipulate a big SQL sentence. 最后,我使用PHP而不是操纵一个大型SQL语句来提供解决方案。
I create the array using the function ObtainArrayFromSQL, and then, use the function array_search() with min() as needle in the haystack (array). 我使用函数ObtainArrayFromSQL创建数组,然后使用函数array_search()和min()作为干草堆中的指针(数组)。
function ObtainArrayFromSQL($arraySQL){
$array = array();
for ($i = 1; $i < 25; $i++)
{
array_push($array, $data_day[$i."h"]);
}
return $array;
}
...
$index_lower_value = array_search(min($array), $array);
Thank you everyone! 谢谢大家!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.