工作中遇到的问题,先记录一下,方便以后查看

存在两张表,user表和friend表

user表部分字段,如上图

friend表部分字段,如上图

往friend表插入千条数据,friend表中的userId值是固定的,对应的friendId是从user表中获取

实现方案:

1、游标存储:

-- 在windows系统中写存储过程时,如果需要使用declare声明变量,需要添加这个关键字,否则会报错。
delimiter //
drop procedure if exists insertUserFriend;
CREATE PROCEDURE insertUserFriend(id int, idx int, size int)
BEGIN
-- 创建接收游标数据的变量
declare c int;
-- 创建结束标志变量
declare done int default FALSE;
-- 创建游标
declare cur cursor for select userId from u_user where userId != id LIMIT idx, size;
-- 指定游标循环结束时的返回值
declare continue HANDLER for not found set done = TRUE;
-- 打开游标
open cur;
-- 开始循环游标里的数据
read_loop:loop
-- 根据游标当前指向的一条数据
fetch cur into c;
-- 判断游标的循环是否结束
if done then
leave read_loop; -- 跳出游标循环
end if;
INSERT INTO u_user_friend (userId, friendId, createTime, lastExchangeTime) VALUES (id, c, NOW(), NOW());
-- 结束游标循环
end loop;
-- 关闭游标
close cur;
-- 输出结果
select * from u_user_friend WHERE userId = id;
END;
//
-- 调用位置修改值就可以,不用重建存储过程
CALL insertUserFriend(7071, 0, 5082);

2、while循环---待测试

DELIMITER;//
create procedure myproc()
begin
declare num int;
declare num1 int;
DECLARE oldUserId int;
DECLARE newUserId int;
DECLARE isExist int;-- 用于验证是否存在好友关系
set num=1;
set num1=1;
set oldUserId=0;
set newUserId=0;
set isExist=0;
while num <= 100 do
select min(userId) into oldUserId from u_user where userId>oldUserId;
set num1=1;
while num1 <= 1000 do
set newUserId=newUserId+1;
select min(userId) into newUserId from u_user where userId>newUserId;
if (newUserId>0 and oldUserId>0)
THEN
select count(userId) into isExist from u_user_friend where (userId=oldUserId and friendId=newUserId) or (userId=newUserId and friendId=oldUserId);
if(isExist=0) THEN
INSERT INTO `u_user_friend`
(`userId`, `friendId`, `memo`, `des`, `img`, `mps`, `black`, `createTime`, `blackTime`, `lastExchangeTime`)
VALUES
(oldUserId, newUserId, NULL, NULL, NULL, NULL, b'', NOW(), NOW(), NOW());
END IF;
END IF;
set num1=num1+1;
set isExist=0;
end while;
set oldUserId=oldUserId+1;
set num=num+1;
end while;
commit;
end;// -- 执行存储过程
CALL myproc(); -- 删除存储过程
-- drop PROCEDURE myproc -- 清空u_user_friend里面的所有数据(慎用)
-- truncate table u_user_friend

附:

delimiter的作用:告诉解释器,这段命令是否已经结束了,mysql是否可以执行了 
默认情况下,delimiter是‘;’但是当我们编写procedure时,如果是默认设置,那么一遇到‘;’,mysql就要执行,这是我们不希望看到的

MySQL往表里插入千条数据 存储过程的更多相关文章

  1. mysql一次插入多条数据

    mysql一次插入多条数据: INSERT INTO hk_test(username, passwd) VALUES ('qmf2', 'qmf2'),('qmf3', 'qmf3'),('qmf4 ...

  2. mysql中造3千条数据(3种方法)

    方法一:存储过程 1.存储过程如下: delimiter $$ DROP PROCEDURE IF EXISTS data CREATE PROCEDURE data(in i int) BEGIN ...

  3. MySql LAST_INSERT_ID 【插入多条数据时】

    LAST_INSERT_ID 自动返回最后一个 INSERT 或 UPDATE 操作为 AUTO_INCREMENT 列设置的第一个发生的值. 参考这里 The ID that was generat ...

  4. WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 项目中有1000万条历史案卷,为某地方坐标系数据,我们的真实 ...

  5. mysql命令行批量插入100条数据命令

    先介绍一个关键字的使用: delimiter 定好结束符为"$$",(定义的时候需要加上一个空格) 然后最后又定义为";", MYSQL的默认结束符为" ...

  6. PHP MySQL 插入多条数据

    PHP MySQL 插入多条数据 使用 MySQLi 和 PDO 向 MySQL 插入多条数据 mysqli_multi_query() 函数可用来执行多条SQL语句. 以下实例向 "MyG ...

  7. 用一条mysql语句插入多条数据

    这篇文章主要介绍了在mysql中使用一条sql语句插入多条数据,效率非常高,但是原理其实很简单,希望对大家有所帮助 假如有一个数据表A: id name title addtime 如果需要插入n条数 ...

  8. 你向 Mysql 数据库插入 100w 条数据用了多久?

    阅读本文大概需要 2 分钟. ▌目录 多线程插入(单表) 多线程插入(多表) 预处理 SQL 多值插入 SQL 事务( N 条提交一次) ▌多线程插入(单表) 问:为何对同一个表的插入多线程会比单线程 ...

  9. 使用 MySQLi 和 PDO 向 MySQL 插入多条数据

    PHP MySQL 插入多条数据 使用 MySQLi 和 PDO 向 MySQL 插入多条数据 mysqli_multi_query() 函数可用来执行多条SQL语句. 以下实例向 "MyG ...

随机推荐

  1. c 链表和动态内存分配

    兜兜转转又用到了c.c的一些基本却忘记的差不多了(笑哭)!! 动态内存分配 当malloc完将返回的指针类型强制转换成想要的类型后,指针中存有该指针的数据结构,而分配的内存恰好可用于该数据结构. 链表 ...

  2. 被称为“开发者神器”的GitHub,到底该怎么用?

    被称为“开发者神器”的GitHub,到底该怎么用? 原文:https://baijiahao.baidu.com/s?id=1594232691312740966&wfr=spider& ...

  3. 使用NPOI按照word模板文件生成新的word文件

    /// <summary> /// 按照word模板文件 生成新word文件 /// </summary> /// <param name="tempFile& ...

  4. 廖雪峰Java7处理日期和时间-2Data和Calendar-1Date

    计算机中如何存储和表示日期和时间 Epoch Time:从1970年1月1日零点(格林威治时区/GMT+00:00)到现在经历的秒数,也叫timestamp, 例如: 秒级: * 北京 2016-11 ...

  5. 基础 - 获得CPU主频

    // 获得cpu主频.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include ...

  6. 20165312 2017-2018-2《Java程序设计》课程总结

    20165312 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...

  7. Linux系统重置root用户密码

    Linux系统重置root用户密码 作者:Eric 微信:loveoracle11g 查看系统版本是不是RHEL7 [root@zhouwanchun ~]# cat /etc/redhat-rele ...

  8. (整理)在REHL6.5上部署ASP.NET MVC

    最近项目要使用Linux服务器(REHL6.5)+MySQL,因此特尝试操作. 1 Linux 安装Jexus 1.1 下载Jexus 因为服务器没有安装Xwindows,Jexus的下载又出现问题, ...

  9. (转发)storm 入门原理介绍

    1.hadoop有master与slave,Storm与之对应的节点是什么? 2.Storm控制节点上面运行一个后台程序被称之为什么?3.Supervisor的作用是什么?4.Topology与Wor ...

  10. 一个简单的通讯服务框架(大家发表意见一起研究)JAVA版本

    最近研究下java语言,根据一般使用的情况,写了个连接通讯服务的框架: 框架结构 C-Manager-S; 把所有通讯内容抽取成三个方法接口:GetData,SetData,带返还的Get; 所有数据 ...