简体   繁体   English

MySQL SQL min()和max()查询数组回显零

[英]mySQL min() and max() query array echoing zero

I have a table set up with 5 columns: 我有一个5列的表格:

user_id, 5k, 10k, halfmarathon, and marathon. user_id,5k,10k,半程马拉松和马拉松。

I want to display the user's best times on a user page for each distance of run. 我想在每个运行距离的用户页面上显示用户的最佳时间。 The user will be able to update any of the times which creates a new row and the rest of the columns are set to null. 用户将可以更新创建新行的任何时间,而将其余列设置为null。 So for example, 例如

Row 1 is: 第1行是:

user_id: 5, 5k: null, 10k: 45:00, half: null, marathon: null. user_id:5、5k:null,10k:45:00,一半:null,马拉松:null。

Then the user runs another 10k and gets a better time, plus wants to update their 5k time : 然后,用户再运行10k并获得更好的时间,再加上要更新其5k的时间:

Row 2 is then: 然后,第2行是:

user_id: 5, 5k: 15:53, 10k: 40:40, half: null, marathon: null. user_id:5、5k:15:53、10k:40:40,一半:null,马拉松:null。

When I run the following SQL query 当我运行以下SQL查询时

$query = "SELECT MIN(5k), MIN(10k), MIN(halfmartahon), MIN(marathon)
        FROM Times
        WHERE user_id = ".$userID."
        GROUP BY user_id";
$db->query($query);
//Assign Result Set
$user_benchmarks = $db->single();`

I get an array that is correct when I vardump() (I am storing the times in seconds) : 当我vardump()时,我得到一个正确的数组(我以秒为单位存储时间):

object(stdClass)#18 (4) { ["MIN(5k)"]=> string(3) "953" ["MIN(10k)"]=> string(4) "2440" ["MIN(halfmarathon)"]=> string(1) "0" ["MIN(marathon)"]=> string(1) "0" } object(stdClass)#18(4){[“” MIN(5k)“] =>字符串(3)” 953“ [” MIN(10k)“] =>字符串(4)” 2440“ [” MIN(halfmarathon) “] =>字符串(1)” 0“ [” MIN(马拉松)“] =>字符串(1)” 0“}

However, when I try to echo this, so $user_benchmarks->5k it doesn't show anything and when I run print_r($user_benchmarks->5k) it comes back as NULL . 但是,当我尝试回显此内容时,因此$user_benchmarks->5k不会显示任何内容,而当我运行print_r($user_benchmarks->5k)它会返回NULL

Has anyone encountered this / know what's happening? 有没有人遇到这个/知道发生了什么? I've also tried turning the string to an integer before printing it to no avail - still get NULL . 我还尝试过将字符串转换为整数,然后再打印无济于事-仍然为NULL

var_dump already showed you exactly what keys to use: var_dump已经向您确切显示了要使用的键:

object(stdClass)#18 (4) { ["MIN(5k)"]=> string(3) "953"
                            ^^^^^^^

so why are you using 那你为什么用

$user_benchmarks->5k
                 ^^^

?

It's because the "5k" property of the $user_benchmarks object doesn't exist. 这是因为$ user_benchmarks对象的“ 5k”属性不存在。 You need to access the "MIN(5k)" property instead. 您需要访问“ MIN(5k)”属性。 So for example: 因此,例如:

echo $user_benchmarks->{"MIN(5k)"};

On the other hand you can change the query to something like this: 另一方面,您可以将查询更改为以下内容:

SELECT MIN(5k) AS `5k`, MIN(10k) AS `10k` ...

Then you will be able to access properties "5k" and "10k" just like you wanted. 然后,您将可以按需要访问属性“ 5k”和“ 10k”。

Use 采用

SELECT MIN(5k) as 5k, 
       MIN(10k) as 10k, 
       MIN(halfmartahon) as halfmartahon, 
       MIN(marathon) as marathon

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

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