MySQL常用操作

MySQL常用操作

1. 插入操作

  • insert into table1 values (value1, value2, value3);   -- 单独插入一行
  • insert into table2 select no, id, score as newnames from table1;  -- 批量插入

2. 删除操作

  • delete from table1 where ...;  -- 只删除表数据,每删除一行会作为一个事务记录在日志中保存以便进行进行回滚操作。(逐条删除,效率较低)如果有trigger会触发
  • truncate table table1; -- 保留表结构(列、约束、索引等),删除表的所有数据,不会记录在日志中,不能回滚(整体删除,效率较高)不会触发trigger,且有foreign key约束引用的表,不能使用truncate table,要使用不带where子句的delete语句
  • drop table table1; -- 表结构和表数据一同删除,不能回滚

3. 更新操作

 更新行

  • update table1 set column = value1 where ....

4. 连表查询操作

  • left join
  • right join
  • full join
  • inner join
  • unionunion all:union all 不去重,union相当于union all加上distinct后的效果,union all效率较高

5. 常用函数

   数学函数

  • abs(-1.2)  --  返回绝对值
  • avg(字段名)  --  返回平均值
  • count(字段名或*)  --  返回行数
  • min(字段名)  --  返回最小值
  • rand()  --  返回0到1的随机数
  • round()  --  返回离x最近的整数
  • sum(字段名)  -- 返回指定字段的总和
  • truncate(x, y)  --  返回数值x保留到小数点后y位的值

  字符串函数

  • concat('My', 'S', 'QL')  --  拼接字符串
  • find_in_set('a', 'a,b,c')  --  精确查询字符串
  • replace(uuid(), '-', '')  --  替换字符
  • length(字符串)  --  返回字符串的长度
  • reverse('abc') -- 反序输出字符串,cba
  • trim(s)  --  去掉字符串 s 开始和结尾处的空格
  • lower(s)  --  将字符串 s 的所有字母变成小写字母
  • upper(s)  --  将字符串转换为大写
  • substr(string, start_index, length)  --  从字符串 s 的 start 位置截取长度为 length 的子字符串
  • str_to_date(string, format_mask)  --  将字符串转变为日期

  日期函数

  • to_days(时间字段) -- 计算日期d 距离0000 年1 月1 日的天数
  • date_format(时间字段, format) -- 格式化输出日期时间
  • date_sub(date,INTERVAL expr type) -- 从日期减去指定的时间间隔
  • yearweek()  --  返回年份及第几周(0到53)
  • period_diff(period1, period2)  --  返回两个时段之间的月份差值 ,period_diff(201709, 201704)
  • year(‘2019-10-31’)  --  获取日期中的年份值2019
  • month(‘2019-10-31’)  --  获取日期中的月份值10
  • day(‘2019-10-31’)  --  获取日期中的月份值31
  • time('2019-10-31 09:05:10')  --  获取日期中的时间部分09:05:10
  • date('2019-10-31 09:05:10')  --  获取日期中的日期部分2019-10-31
  • now()  -- 获取当前日期和时间
  • curdate()  -- 获取当前日期
  • curtime() = date_format(now(), '%H:%m:%s'); --  获取当前时间部分

  常用查询语句

  • 查询当天数据:
    select * from 表名 where TO_DAYS(表中时间字段) = TO_DAYS(now()); 
  • 查询昨天数据:
    select * from 表名 where TO_DAYS(now()) - TO_DAYS(表中时间字段) = 1;
  • 查询当周数据:
    select * from 表名 where YEARWEEK(date_format(表中时间字段, '%Y-%m-%d')) = YEARWEEK(now());
  • 查询当月数据:
    select * from 表名 where DATE_FORMAT(时间字段名, '%Y%m') = DATE_FORMAT(curdate(), '%Y%m');
  • 查询最近一周数据:
    select * from 表名 where DATE_SUB(curdate(), INTERVAL 7 DAY) <= date(表中时间字段);
    select * from 表名 where DATE_SUB(curdate(), INTERVAL 1 week) <= date(表中时间字段);
  • 查询最近一个月内的数据:
    select * from 表名 where DATE_SUB(curdate(), interval 1 month) <= date(表中时间字段);
    select * from 表名 where DATE_SUB(CURDATE(), interval 30 day) <= date(时间字段名);
  • 查询上个月的统计数据:
    select * from 表名 where PERIOD_DIFF(date_format(now(), '%Y%m' ), date_format(时间字段名, '%Y%m')) =1;
  •  查询当年每月的统计数据:
    select MONTH(`表中日期字段`), count(*) from 表名 where YEAR(curdate()) group by MONTH(`表中日期字段`);
  • 查询本季度数据:

    select * from 表名 where QUARTER(create_date)=QUARTER(now());
  • 查询上季度的数据:

    select * from 表名 where QUARTER(表中时间字段)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)) and year(create_time)=year(now());
  • 查询本年数据:

    select * from 表名 where YEAR(表中时间字段)=YEAR(NOW());
  • 查询上年数据:

    select * from 表名 where year(表中时间字段)=year(date_sub(now(),interval 1 year));

【样本数据】

create table demo_t(
    `oid` varchar(36) not null,
    `no` varchar(10) not null,
    `name` varchar(20) not null,
    `create_time` TIMESTAMP not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
    primary key(oid)
) ENGINE=InnoDB default charset=utf8; 

insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1001', '台灯');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1002', '灯泡');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1003', '灯座');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1004', '空调');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1005', '香薰机');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1006', '热水器');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1007', '净水器');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1008', '加湿器');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1009', '空气检测器');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1010', '空气净化器');
insert into demo_t (oid, no, name) values(replace(uuid(), '-', ''), '1011', '音箱');

update demo_t set create_time='2018-09-20 18:49:57' where no = '1001';
update demo_t set create_time='2019-09-22 18:49:57' where no = '1002';
update demo_t set create_time='2019-08-30 18:49:57' where no = '1003';
update demo_t set create_time='2019-10-01 18:49:57' where no = '1004';
update demo_t set create_time='2019-10-04 18:49:57' where no = '1005';
update demo_t set create_time='2019-10-08 18:49:57' where no = '1006';
update demo_t set create_time='2019-10-10 18:49:57' where no = '1007';
update demo_t set create_time='2019-10-18 18:49:57' where no = '1008';
update demo_t set create_time='2019-10-19 18:49:57' where no = '1009';
update demo_t set create_time='2019-10-20 18:49:57' where no = '1010';
update demo_t set create_time='2019-07-20 18:49:57' where no = '1011';

 

posted @ 2019-10-20 11:43  老夫的少女心  阅读(211)  评论(0编辑  收藏  举报