当前位置:网站首页>存储过程 两次排序加一次随机取数据
存储过程 两次排序加一次随机取数据
2022-07-23 05:40:00 【我王十一呀】
sql
过程逻辑 先根据id倒序查询到2000条数据,再从这1000条数据中根据金额从大到小查出100条数据,且这100条数据随机取出
CREATE DEFINER=`root`@`%` PROCEDURE `NewProc`()
BEGIN
-- 临时表存在就删除
DROP TABLE IF EXISTS SalesSummary;
CREATE TEMPORARY TABLE SalesSummary (
id INT NOT NULL,
user_id INT NOT NULL DEFAULT 0.00,
money DECIMAL(12,2) NOT NULL DEFAULT 0.00,
remark VARCHAR(50) NOT NULL ,
note VARCHAR(50) NOT NULL,
type TINYINT(3) NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
money_type VARCHAR(1) NOT NULL,
business_id INT NOT NULL,
nickname VARCHAR(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
-- 临时表存在就删除
DROP TABLE IF EXISTS SalesSummaryB;
CREATE TEMPORARY TABLE SalesSummaryB (
id INT NOT NULL,
user_id INT NOT NULL DEFAULT 0.00,
money DECIMAL(12,2) NOT NULL DEFAULT 0.00,
remark VARCHAR(50) NOT NULL ,
note VARCHAR(50) NOT NULL,
type TINYINT(3) NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
money_type VARCHAR(1) NOT NULL,
business_id INT NOT NULL,
nickname VARCHAR(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
-- 将那一百条数据放到临时表
INSERT INTO SalesSummary select a.id,a.user_id,a.money,a.remark,a.note,a.type,a.created_at,a.updated_at,a.money_type,a.business_id,b.nickname from money_logs as a left join users as b on a.user_id=b.id WHERE `type`=1 or `type`=4 or `type`=5 order by id desc limit 2000;
-- 根据money获取10条数据到临时表
INSERT INTO SalesSummaryB select * from SalesSummary order by money desc limit 100;
-- 增加字段
ALTER TABLE SalesSummaryB ADD temp_id INT NOT NULL auto_increment primary key;
-- ALTER TABLE SalesSummaryB ADD nickname VARCHAR(50);
-- 随机打乱临时表表中的全部数据
select * from SalesSummaryB order by rand() limit 100;
END
php
$asc = DB::select('call NewProc()');
边栏推荐
- 视图集及路由
- Redis database and project framework
- Pycharm occupies C disk
- 【6.28】
- After the formula in word in WPS is copied, there is a picture
- 高阶函数的应用:手写Promise源码(四)
- Rice mall registration
- 【C语言】什么是函数?函数的分类和侧重(帮你快速分类和记忆函数)
- my_ Implementation of strcpy (classic, simple, practical, collection)
- npm init vite-app <project-name> 报错 Install for [‘[email protected]‘] failed with code 1
猜你喜欢
随机推荐
D2dengine edible tutorial (1) -- the simplest program
Redis数据库和项目框架
General view, serializer
Paging and filtering
Flask blueprint
Differences and basic operations of shell/sh/bash
分页、过滤
Rice mall registration
使用require.context完成本地图片批量导入
第一篇博客
Copy a project /project in idea
When using cache in sprintboot, the data cannot be loaded
JWT header for coding process
Application of higher-order functions: handwritten promise source code (II)
Custom formula input box
Scattered notes of machine learning: some concepts and notes
JS, pay attention to passing parameters. If it is a string, you need to add escape characters
Spark常见面试问题整理
Common errors in C language debugging -- brief answer
Pycharm occupies C disk








