MySQL的一些常用SQL语句

xiaoxiao2021-02-28  109

备份表: create table 新表  select *  from 旧表 创建临时表: CREATE TEMPORARY TABLE tableName (id varchar(100),name varchar(100),age varchar(100)) 删除临时表: DROP TEMPORARY TABLE IF EXISTS tableName case用法:lysl=lysl-(case yzt when '领用' then sl else 0 end)           lysl=lysl-(case when yzt='领用' then sl when yzt='报废' then 0 else 1 end)

批量插入数据:insert into table select * from oldtable

MySQL的空和null转换:

字段空就是空:ifnull(NULLIF(字段,''),'') 字段为null就是空:ifnull(字段,'') 子字段为空就是null:nullif(字段,'')

字符串截取:

1、从左开始截取字符串  left(str, length)  说明:left(被截取字段,截取长度)  例:select left(content,200) as abstract from my_content_t  2、从右开始截取字符串  right(str, length)  说明:right(被截取字段,截取长度)  例:select right(content,200) as abstract from my_content_t  3、截取字符串  substring(str, pos)  substring(str, pos, length)  说明:substring(被截取字段,从第几位开始截取)  substring(被截取字段,从第几位开始截取,截取长度)  例:select substring(content,5) as abstract from my_content_t  select substring(content,5,200) as abstract from my_content_t  (注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度)  4、按关键字截取字符串  substring_index(str,delim,count)  说明:substring_index(被截取字段,关键字,关键字出现的次数)  例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t  结果:blog.jb51  (注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)

转载请注明原文地址: https://www.6miu.com/read-30516.html

最新回复(0)