mysql常用sql语句

来源:互联网 发布:淘宝运营vip课程 云盘 编辑:程序博客网 时间:2024/05/22 15:52

1,备份表

select * into new_table from old_table 

说明:new_table不存在,但mysql不支持


create table new_table select * from old_table 

说明:mysql版本在5.6以上(默认开启了GTID的限制)的会报Statement violates GTID consistency的错误,5.6版本以下能正常执行。(select version();)

GTID(global transaction identifier)全局事务ID,主从复制使用,保证每个在主库上提交的事务在集群中有唯一的ID,只允许能够保证事务安全,且能被日志记录的sql语句执行。

5.6以上解决方案是拆分语句为

create table  new_table like old_table;

insert into new_table select * from old_table;(new_table需已存在)


2,创建临时表

create temporary tabel tmp_table select * from table_name


3,连表更新

update table1 inner join table2 on table1.id=table2.id set table1.name=table2.name where table1.id>3

update table1,table2 set table1.name=table2.name where table1.id=table2.id and table1.id>3


4,截取字符串函数

update tb_memberaddress set detailAddress = SUBSTRING_INDEX(detailAddress,"|",-1) where id = 22

说明:substring_index(str1,str2,index)

str1:待截取的字符串

str2:特殊字符,定位截取位置

index; 如-2,意思是将str1从左开始截取到str1中第二次出现特殊字符str2的地方。


原创粉丝点击