mysql常用sql语句

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的地方。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值