各大平台店鋪的三項評分(物流、服務、商品)變化情況;
商品每日**的變化記錄;
**的實時漲跌浮;
表:主鍵id,商品編號,記錄時的時間,記錄時的**,建立時間。
問題:獲取每個商品每次的變化情況(漲跌幅、漲跌率)。
1、要想高效率的更新漲跌,就肯定不能是逐條資料更新,要通過自連表建立起對應關係,將每一條資料關聯到上一次的**資料。
2、由於資料庫非常龐大,所以可能存在很多垃圾資料,就比如說相關的字段值為null或者非有效值的,這些資料要先排除掉。
select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null;
3、然後在獲取每條資料的上一條資料,同樣也要先排除掉垃圾資料。
select tmp_a.*, max(tmp_b.goods_date) as last_date from
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_a
left join
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_b
on tmp_a.goods_code = tmp_b.goods_code and tmp_a.goods_date > tmp_b.goods_date group by tmp_a.id;
4、獲取到上一條資料後,獲取上條資料對應的商品**。
select tmp_ab.*,tmp_c.goods_price as last_price from
( select tmp_a.*, max(tmp_b.goods_date) as last_date from
( select id,goods_code,goods_date,goodsbxvuucbb_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_a
left join
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_b
on tmp_a.goods_code = tmp_b.goods_code and tmp_a.goods_date > tmp_b.goods_date group by tmp_a.id
) as tmp_ab
left join (select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_c
on tmp_ab.www.cppcns.comgoods_code = tmp_c.goods_code and tmp_c.goods_date = tmp_ab.last_date order by tmp_ab.id;
5、獲取到上條資料以及對應的**後,開始進行計算,獲取到最終的結果。
select
*, (convert(goods_price, decimal(10,2)) - convert(last_price, decimal(10,2))) as '漲跌幅',
round((convert(goods_price, decimal(10,2)) - convert(last_price, decimal(10,2)))/convert(last_price, decimal(10,2)), 2) as '漲跌率'
from (
select tmp_ab.*,tmp_c.goods_price as last_price from
( select tmp_a.*, max(tmp_b.goods_date) as last_date from
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_a
left join
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_b
on tmp_a.goods_code = tmp_b.goods_code and tmp_a.goods_date > tmp_b.goods_date group by tmp_a.id
) as tmp_ab
left join (select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_c
on tmp_ab.goods_code = tmp_c.goods_code and tmp_c.goods_date = tmp_ab.last_date order by tmp_ab.id
) as tmp
-- 建立表sql
create table `test_goods_price_change` (
`id` int(11) not null auto_increment comment '主鍵id',
`goods_code` varchar(50) not null comment '商品編碼',
`goods_date` int(11) not null comment '記錄時的時間',
`goods_price` decimal(10,2) not null comment '記錄時的**',
`created_at` int(11) not null comment '建立時間',
primary key (`id`)
) engine=innodb charset=utf8mb4;
-- 獲取漲跌浮sql
select
*, (convert(goods_price, debxvuucbbcimal(10,2)) - convert(last_price, decimal(10,2))) as '漲跌幅',
round((convert(goods_price, decimal(10,2)) - convert(last_price, decimal(10,2)))/convert(last_price, decimal(10,2bxvuucbb)), 2) as '漲跌率'
from (
select tmp_ab.*,tmp_c.goods_price as last_price from
( select tmp_a.*, max(tmp_b.goods_date) as last_date from
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_a
left join
( select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_b
on tmp_a.goods_code = tmp_b.goods_code and tmp_a.goods_date > tmp_b.goods_date group by tmp_a.id
) as tmp_ab
left join (select id,goods_code,goods_date,goods_price from test_goods_price_change where goods_price is not null and goods_date is not null ) as tmp_c
on tmp_ab.goods_code = tmp_c.goods_www.cppcns.comcode and tmp_c.goods_date = tmp_ab.last_date order by tmp_ab.id
) as tmp
用一句sql語句更新兩個表並可更新對應的字段的值
例子 insert into products pronumber,casnumber,cnname,price,enname,baozhuang,pinpai select pronumber,casnumber,cnname,price,enname,baozhuang,pinpai from ...
用一句sql語句更新兩個表並可更新對應的字段的值
用一句sql語句更新兩個表並可更新對應的字段的值 access 例子 insert into products pronumber,casnumber,cnname,price,enname,baozhuang,pinpai select pronumber,casnumber,cnname,pri...
一句SQL實現MYSQL的遞迴查詢
建立 create table treenodes id int 節點id nodename varchar 60 節點名稱 pid int 節點父id 插入測試資料 insert into treenodes id nodename pid values 1 a 0 2 b 1 3 c 1 4 d...