简体   繁体   English

使用PHP将MySQL查询存储在另一个表中

[英]store mysql query in another table using php

This is my MySQL table feedback . 这是我的MySQL表feedback

Mysql表

I want to calculate average of every column and store the result to table average as below structure using a PHP file. 我想计算每列的平均值,并使用PHP文件将结果存储到表average ,如下结构所示。

在此处输入图片说明

I am using query SELECT AVG (waiting) FROM feedback to calculate average of waiting column. 我正在使用查询SELECT AVG (waiting) FROM feedback来计算等待列的平均值。 But I do not have any idea of how to put this query result to another table as the above structure. 但是我不知道如何将查询结果作为上述结构放置到另一个表中。

When using query INSERT INTO average (average) SELECT AVG (waiting) FROM feedback **it only updating the average column. 当使用查询INSERT INTO average (average) SELECT AVG (waiting) FROM feedback **它仅更新平均列。

Please help me with a sample code. 请帮我提供示例代码。

Thanks 谢谢

If understand you correctly you can do it like this 如果正确理解您可以这样做

INSERT INTO average (data, average)
SELECT 'waiting',      AVG (waiting)      FROM feedback UNION ALL
SELECT 'consultation', AVG (consultation) FROM feedback UNION ALL
SELECT 'preoperative', AVG (preoperative) FROM feedback 

Sample output: 样本输出:

|         DATA | AVERAGE |
--------------------------
|      waiting |       3 |
| consultation |       2 |
| preoperative |       3 |

Here is SQLFiddle demo 这是SQLFiddle演示


Now if you don't have lots of data in feedback table you can ditch average table and create a view with the same way. 现在,如果feedback表中没有太多数据,则可以放弃average表并以相同方式创建视图。 As a result instead of maintaining a table of averages they will be calculated on the fly when you select from it 因此,当您从中进行选择时,它们会即时计算,而不是维护平均值表

CREATE VIEW average AS 
SELECT 'waiting',   ROUND(AVG (waiting)) average FROM feedback UNION ALL
SELECT 'consultation', ROUND(AVG (consultation)) FROM feedback UNION ALL
SELECT 'preoperative', ROUND(AVG (preoperative)) FROM feedback

And use it 并使用它

SELECT * FROM average

Here is SQLFiddle demo 这是SQLFiddle演示

You can grab the result from the average of each column and then INSERT that into a separate database. 您可以从每列的平均值中获取结果,然后将其插入到单独的数据库中。

What you are doing now inserts it into the same column in the same database, not a different one, which is why it's just updating that column and not a different one. 现在,您正在执行的操作将其插入到同一数据库的同一列中,而不是其他数据库中,这就是为什么它只是更新该列而不是另一列。

Example of an INSERT query: INSERT查询的示例:

$query = "INSERT INTO tableName (columnName) VALUES ($averageOfColumn)";

Replace 'tableName' with the name of the new table you are connecting to. 将“ tableName”替换为您要连接的新表的名称。

Replace 'columnName' with the name of the column in the other database you are using. 将“ columnName”替换为您正在使用的其他数据库中的列名。

Replace '$averageOfColumn' with your variable for the average of the waiting column. 将“ $ averageOfColumn”替换为变量,以获取等待列的平均值。

Does this help? 这有帮助吗?

To get average values for those three columns: 要获得这三列的平均值:

SELECT AVG(waiting),AVG(consultation),AVG(preoperative) FROM feedback;

To insert to average table, if column names are the same: 如果列名相同,则要插入到平均值表中:

INSERT INTO average(waiting,consultation,preoperative) 
SELECT AVG(waiting),AVG(consultation),AVG(preoperative) FROM feedback

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

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