简体   繁体   English

如何使用PHP中的一个mySQL数据库表处理两组大型数据?

[英]How to work with two large sets of data from one mySQL database table in PHP?

I'm attempting to work with two sets of data from the same mySQL table in a PHP script. 我正在尝试在PHP脚本中使用来自同一mySQL表的两组数据。 The idea is data is scraped from an API and into a database hourly. 这个想法是每小时从API抓取数据到数据库中。 A second script then pulls the information out of the database and displays a rolling 6-hour delta. 然后,第二个脚本将信息从数据库中拉出,并显示6小时滚动增量。

I've run into a bit of a problem trying to create the delta from the two datasets. 尝试从两个数据集创建增量时遇到了一个问题。 I need to run two mySQL queries to get the data I need (current and from 6 hours ago), but can't really think of a way to get the script to work without including the queries inside the loops that output each entry (These can run up to a couple of hundred times, and I don't think having that many mySQL queries running would be good?) 我需要运行两个mySQL查询来获取所需的数据(当前和6个小时前的数据),但是在没有将查询包含在输出每个条目的循环内的情况下,我真的想不出一种方法来使脚本工作(这些最多可以运行数百次,并且我认为运行许多mySQL查询不会很好吗?)

This is what I have so far: 这是我到目前为止的内容:

//select the system table and pulls data acquired within the last hour.
$sql = "SELECT system, vp, vpthreshold, owner, time FROM SysData WHERE time > DATE_SUB(NOW(),INTERVAL 1 HOUR)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

    //Calculates contested percentage
    $vpthreshold = $row["vpthreshold"]; 
    $vp = $row["vp"];   
    $currentcontested = $vp/$vpthreshold*100;

    //Catches potential divide by zeroes, echos system is stable.
    if ($vp == 0.0){
        echo $row["system"] . " " . "is Stable<br>";
    }
    //else output contested percentage with system name in readable format.
    else{

        echo $row["system"] . " " . "{$currentcontested}%" . "<br>";
    }
} 
}

There's a broadly identical statement that pulls and echos the second set of information underneath this. 有一个大致相同的语句可以提取并回显其下的第二组信息。 How can I get these two sets together so I can work with them? 如何将这两套设备组合在一起,以便与它们一起工作? Very new to PHP and learning on the fly here. PHP的新手,可在这里进行动态学习。

You can look into nested queries. 您可以调查嵌套查询。 Something like the following: 类似于以下内容:

SELECT (data_now.somevalue - data_prev.somevalue) as deltavalue FROM
(
    (your first select statement) as data_now, 
    (your 6 hours ago select statement) as data_prev
);

This lets you select data from other select statements all in one go. 这使您可以一次从其他select语句中选择数据。 The 2 inner "select statements" you should replace with your respective queries. 您应将2个内部“ select语句”替换为各自的查询。 The results will be put temporarily into data_now and data_prev. 结果将暂时放入data_now和data_prev中。 You can then use these as normal tables in the outer select statement. 然后,您可以在外部select语句中将它们用作普通表。

EDIT: To be more specific to what you want, here is an updated example: 编辑:要更具体地想要什么,这是一个更新的示例:

SELECT (data_now.vp/data_now.vpthreshold - data_prev.vp/data_prev.vpthreshold) as deltavalue FROM
(
    (SELECT system, vp, vpthreshold, owner, time FROM SysData WHERE time > DATE_SUB(NOW(),INTERVAL 1 HOUR)) as data_now, 
    (your 6 hours ago select statement) as data_prev
);

In your PHP code remember to reference the result as: 在您的PHP代码中,请记住将结果引用为:

$row["deltavalue"]

or whatever you put after "as" in the outer SELECT. 或在外部SELECT中放在“ as”之后的内容。

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

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