常用sql随笔记

sql 替换字段中的部分字符,替换指定字符。例:把列中凡是有2011的全部修改成2014

update tb_zbxxid set zdgc = replace(zdgc,'2011','2014')

mysql左右截取

select left('字段',5) 
select right('字段',5) 

trim 去除前后空格

日期函数

day()
month()
year()
dateiff(day,dateStr1,dateStr2) 

数值函数

round(3.141593,2)  四舍五入
rand() 产生随机数  例 order by rand() linit 5
count(*) 计数所有
count(字段) 制定字段  null是统计不到的

having 和聚合函数结合使用

group by  字段 having where  字段>'值'

inner join 内链接 2个表同时都存在
left join 外连接 主表的数据必有

正则查询 regexp

regexp '^[0-9{18}$]'

你可能感兴趣的:(常用sql随笔记)