计算平均值、添加日期并将查询保存到新 table
Calculate average, add date, and save query to a new table
我有一个名为 pt_products 的 table 字段:
`id` int(6) unsigned NOT NULL,
`ean` bigint(13) unsigned NOT NULL,
`merchant` varchar(64) NOT NULL,
`price` decimal(10,2)NOT NULL,
PRIMARY KEY (`id`)
table 上的条目示例:
INSERT INTO `pt_products` (`id`, `ean`, `merchant`, `price`) VALUES
('1', '884116311874', 'Amazon', '10000.00'),
('2', '884116311874', 'BestBuy', '10999.00'),
('3','884116321378', 'CyberPuerta', '14789.00'),
('4', '884116311875', 'Amazon', '10999.00'),
('5', '884116311875', 'BestBuy', '10000.00');
我想根据 pt_products 上已有的数据创建一个名为 'graph' 的新 table,其中包含以下字段:
-ean代码。
- 共享相同 'ean' 的每个条目的 'price' 字段的平均值
- 条目添加到新 table 的日期,自动添加。
我试过的(demo):
SELECT AVG(price)
FROM pt_products
GROUP BY ean;
我得到:
AVG(price)
929.500000
3697.333333
3834.000000
9999.990000
10499.500000
10499.500000
14789.000000
这样我就得到了具有相同 ean 的条目的平均价格,但我看不到与平均值对应的 'ean'。当然,我不会将其存储到新的 table,包括启动查询的日期。
这样做的目的是获取每个 ean 和每一天的平均价格,以便根据显示价格随时间变化的数据制作图表,所以我需要平均的检索日期。
谢谢
只需将 ean
列添加到 select
子句:
select ean, avg(price) avg_price
from pt_products
group by ean
您可以直接根据查询结果创建新的 table:
create table graph as
select ean, avg(price) avg_price, now() created_at
from pt_products
group by ean
但这并不能让您微调 table 选项(主键、索引...)。最好先创建 table,然后在其中创建 insert
- 您可以设置默认为当前时间点的时间戳:
create table graph (
id int primary key auto_increment,
ean bigint(13) unsigned not null,
avg_price decimal(10,2) not null,
created_at timestamp default current_timestamp
);
insert into graph(ean, avg_price)
select ean, avg(price) avg_price
from pt_products
group by ean;
我有一个名为 pt_products 的 table 字段:
`id` int(6) unsigned NOT NULL,
`ean` bigint(13) unsigned NOT NULL,
`merchant` varchar(64) NOT NULL,
`price` decimal(10,2)NOT NULL,
PRIMARY KEY (`id`)
table 上的条目示例:
INSERT INTO `pt_products` (`id`, `ean`, `merchant`, `price`) VALUES
('1', '884116311874', 'Amazon', '10000.00'),
('2', '884116311874', 'BestBuy', '10999.00'),
('3','884116321378', 'CyberPuerta', '14789.00'),
('4', '884116311875', 'Amazon', '10999.00'),
('5', '884116311875', 'BestBuy', '10000.00');
我想根据 pt_products 上已有的数据创建一个名为 'graph' 的新 table,其中包含以下字段:
-ean代码。 - 共享相同 'ean' 的每个条目的 'price' 字段的平均值 - 条目添加到新 table 的日期,自动添加。
我试过的(demo):
SELECT AVG(price)
FROM pt_products
GROUP BY ean;
我得到:
AVG(price)
929.500000
3697.333333
3834.000000
9999.990000
10499.500000
10499.500000
14789.000000
这样我就得到了具有相同 ean 的条目的平均价格,但我看不到与平均值对应的 'ean'。当然,我不会将其存储到新的 table,包括启动查询的日期。
这样做的目的是获取每个 ean 和每一天的平均价格,以便根据显示价格随时间变化的数据制作图表,所以我需要平均的检索日期。
谢谢
只需将 ean
列添加到 select
子句:
select ean, avg(price) avg_price
from pt_products
group by ean
您可以直接根据查询结果创建新的 table:
create table graph as
select ean, avg(price) avg_price, now() created_at
from pt_products
group by ean
但这并不能让您微调 table 选项(主键、索引...)。最好先创建 table,然后在其中创建 insert
- 您可以设置默认为当前时间点的时间戳:
create table graph (
id int primary key auto_increment,
ean bigint(13) unsigned not null,
avg_price decimal(10,2) not null,
created_at timestamp default current_timestamp
);
insert into graph(ean, avg_price)
select ean, avg(price) avg_price
from pt_products
group by ean;