gpt4 book ai didi

php - 获取多行并存储到变量。 MySQL 和 PHP

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

我正在为我的系统创建一个平均公里运行计算器。所以我试图获取最后两个日期和最后两个在我的数据库上运行的日期。

这是我当前的代码:

<?php

$conn = mysqli_connect('localhost','root','','mypms');

$sqldates = "SELECT dateinput FROM sched ORDER BY dateinput DESC LIMIT 2";
$sqlrun = "SELECT reading FROM sched ORDER BY reading DESC LIMIT 2";

$resultdate = mysql_query($conn,$sqldates);
$resultrun = mysql_query($conn,$sqlrun);


while($rowdate = mysql_fetch_assoc($resultdate))
{
this ->
}

$date_difference = $date2 - $date1;

while($rowrun = mysql_fetch_assoc($resultrun))
{
this ->
}

$run_difference = $run2 - $run1;
$averagerun = $run_difference/$date_difference

echo $averagerun;
?>

我应该在 this 上写什么,以便我可以将 $resultrun 和 $resultdate 存储到 $date1、$date2 和 $run1、$run2。

注意:日期输入的日期类型是我的mysql数据库上的日期。

最佳答案

尝试此查询 - 它将返回您的结果

SELECT 
run_difference/date_difference AS averagerun
FROM
(
SELECT
DATEDIFF(date2,date1) AS date_difference,
reading2 - reading1 AS run_difference
FROM
(
SELECT
(SELECT dateinput FROM sched ORDER BY dateinput LIMIT 0, 1) AS date1,
(SELECT dateinput FROM sched ORDER BY dateinput LIMIT 1, 1) AS date2,
(SELECT reading FROM sched ORDER BY reading DESC LIMIT 0, 1) AS reading1,
(SELECT reading FROM sched ORDER BY reading DESC LIMIT 1, 1) AS reading2
FROM DUAL
) t1
)t2

或“单行”:

SELECT 
(SELECT reading FROM sched ORDER BY reading DESC LIMIT 1, 1)-(SELECT reading FROM sched ORDER BY reading DESC LIMIT 0, 1) / DATEDIFF((SELECT dateinput FROM sched ORDER BY dateinput LIMIT 1, 1), (SELECT dateinput FROM sched ORDER BY dateinput LIMIT 0, 1)) AS averagerun
FROM DUAL

关于php - 获取多行并存储到变量。 MySQL 和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367341/

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