本文介绍了SQL中批量插入数据的两种方式:insert into select和insert on duplicate key update,以及如何使用union和union all进行数据合并。同时,讲解了如何通过SQL更新查询来同步数据,并提供了计算经纬度距离的公式。最后,展示了统计查询的使用,包括limit分页和统计总数。
摘要由CSDN通过智能技术生成
批量 insert
1. 查询后插入 insert into select
insertinto A(col1, col2)select col1, col2 from B;
2. 批量插入更新 insert into on duplicate key update
INSERTINTO t
(id, age)VALUES(3,28),(4,29)ONDUPLICATEKEYUPDATE
id =VALUES(id),
age =VALUES(age);
Union & Union all用法
• Union 合并并去重
• Union all 合并不去重
update select语句
UPDATE bkb_seller bs
INNERJOIN(SELECT new_user,seller_id,create_time from lc_seller_change_history
where id =11) a
SET bs.user_id =a.new_user,bs.create_time = a.create_time
where bs.id = a.seller_id
经纬度距离计算
Distance = fmt.Sprintf("select ROUND(6378.138*2*ASIN(SQRT(POW(SIN((%s*PI()/180-latitude*PI()/180)/2),2)+COS(%s*PI()/180)*COS(latitude*PI()/180)*POW(SIN((%s*PI()/180-longitude*PI()/180)/2),2))),1) AS 'distance' from table1", t.Latitude, t.Latitude, t.Longitude)
统计全部,输出limit
SELECT SQL_CALC_FOUND_ROWS name FROM table1 where name like'%name%'limit10,0;SELECT FOUND_ROWS()'nums';