简体   繁体   English

MySQL临时表

[英]MySQL temporary table

In the project I'm working I use php language and mysql database. 在项目中,我正在使用php语言和mysql数据库。

The reason I thought to use temporary tables was right is because I perform many calculations, and in javascript they would take me a long time. 我认为使用临时表的原因是正确的,因为我执行了许多计算,而在javascript中,它们将花费我很长时间。

I must show these tables at screen: . 我必须在屏幕上显示这些表:

Table 1: 表格1:

Total data.         |   6
Average.            |   2984-99
Sum x-Media.        |   5088.9
Sum (x-Media)^2.    |   138092659.8396
Sum (x-Media)^4.    |   1.09E+16
Maximum value.      |   9812.12
Minimum value.      |   18.23

Table 2: 表2:

Amount  |   x-Media     |   (x-Media)^2     |   (x-Media)^4
839.12  |   -2145.87    |   704122.3744     |   495788318130.694
18.23   |   18.23       |   332.3329        |   110445.15642241
9812.12 |   11957.99    |   96277698.8944   |   9.27E+15
23.93   |   5.7         |   572.6449        |   327922.18149601
863.21  |   -11094.78   |   745131.5041     |   555220958402.328
6353.33 |   6347.63     |   40364802.0889   |   1.63E+15

All calculations are based on the amount column, this value. 所有计算均基于金额列(此值)。 I get it from the table tbl_layout_c: 我从表tbl_layout_c中获得它:

Amount decimal (18,2)
----------
839.12
18.23
9812.12
23.93
863.21
6353.33

So the query is: 因此查询为:

1 SELECT amount 
2 (SELECT COUNT(*) FROM tbl_layout_c ) AS total_data
3 (SELECT SUM(amount) FROM tbl_layout_c ) AS total_sum,
4 (SELECT total_sum / total_data AS average,
5 (SELECT amount - average ) AS x_media,
6 (SELECT SUM(x_media)) AS suma_x_media
7 FROM tbl_layout_c

The issue is at LINE 6: (SELECT SUM(x_media)) AS suma_x_media because I get the same value as x_media column . 问题出在第6行:(SELECT SUM(x_media))AS suma_x_media,因为我得到的值与x_media column相同 Ex: 例如:

Amount      |   x-Media         |   Sum x-Media.
839.12      |   -2145.87        |   -2145.87

And I need: 我需要:

Amount      |   x-Media         |   Sum x-Media.
839.12      |   -2145.87        |   5088.9

I have trouble when I try to use function sum() in the temporary column (suma_x_media) of the temporary table.. It's not working, I just get the same value as x_media 我在临时表的临时列(suma_x_media)中尝试使用函数sum()时遇到麻烦。它不起作用,我得到的值与x_media相同

Anyone have any idea what I'm missing? 有人知道我想念什么吗?

Here you can see an example of the Code 在这里,您可以看到代码示例

try this 尝试这个

select *,sum(x_media) from (SELECT monto, #obtengo el campo monto
(SELECT COUNT(*) FROM tbl_layout_c ) AS total_datos, #contar los registros
(SELECT SUM(monto) FROM tbl_layout_c ) AS suma_total, #suma total
(SELECT MAX(monto) FROM tbl_layout_c ) AS valor_maximo, #valor maximo
(SELECT MIN(monto) FROM tbl_layout_c ) AS valor_minimo, #valor minimo
(SELECT suma_total / total_datos) AS promedio, #promedio
(SELECT monto - promedio) AS x_media #media artimetica
FROM tbl_layout_c)tablealias

I tried in sql Fiddle with your data and It works. 我尝试在SQL Fiddle中使用您的数据,并且可以正常工作。 your sum(x_media) is 0 with data in sql Fiddle you have if you change the data same as here in TABLE 2 then you can get expected output same mentioned in Table 1. 您的sum(x_media)在sql Fiddle中为0,如果您更改表2中的数据,则可以获得与表1中相同的预期输出。

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

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