• mysql 索引+函数查询


    1.建立索引
    create table t1(
    tname varchar(30),
    key (tname) -- 建立索引的第一种方式
              key tt(tname asc)  tt为索引的名字         
    );
    (2)增加主键约束会自带索引
    (3)-- 为tname 字段 建立索引名字为tn
             create index tn on t1(tname desc);
    (4)create unique index tn on t4(tname desc);-- 建立唯一索引
    (5)alter table t add index (tname asc);
       (5.1)alter table t4 add index (a);
    2.项目一开始不适合建立索引,索引的优点:提高检索速度快,但是数据量不大规模小的不适合建立索引,
    缺点:降低数据插入,数据插入修改的速度比较慢,一般的项目刚开始不需要加索引,如果是查找一群的数据也不适合索引

    4.--  删除索引
    drop index tname on t1;   
    drop index a on t4;--a 为索引的名字
    5.length 返回字段字节数
    -- utf 一个字是3个字节, gbk 一个字2个字节,高端的项目建议用gbk  length 获得字节数
    select tid,tname,length(tname) from teacher;
    6.char_length 返回字段的字的个数(支持多字节)
    7.-- 查询有几个字 char_length
    select tname ,char_length(tname) from teacher; 

    utf8 一个汉字为3个字节,gbk一个汉字为2个字节
    select tname,length (tname)from teacher; 

    char varchar是边长的

    create table t4(
    a  char(6),
    b varchar(6)
    );
    select * from t4;
    insert into t4 values ('李d','李d');
    truncate t4; -- 清空t4
    select a,b ,length(a),length(b) from t4;


    8.-- 五天后 
    select date_add(curdate(),interval +5 day);当前日期为4/29

    -- 五天前  curdate()=now() 
    select date_add(curdate(),interval -5 day);
    9.-- 永远是7天内的会员注册信息个数  date_add(时间,增量)返回一个新的日期
    select count(*) from member where regdate between date_add(curdate(),interval -7 day)and curdate();
    10.查询系统日期
    select curdate(); -- 当前系统日期

    select now();-- 当前系统日期
    select uuid(); -- 全球唯一字符串
    11.  rund 随机
    -- 返回0-1直接的随机小数
    select rand();
    select rand () from dual;
    -- round()四舍五入取整数
       select round (rand()*5)+0; -- 0-5
    -- 随机查询两个记录,不建议使用,查询缓存不能用效率低
    select *from student order by rand() limit 2;
    -- 查出来的信息随机排列
    select *from student order by rand() ;



    -- datediff 两个日期之间的差值,大的日期写在前面
    select datediff('2015-8-1','2015-3-5');
    --  timestampdiff()距离今天有几天时间今天的时间为4-29
    select timestampdiff(day,'2015-4-28',curdate());

    -- timestamp diff()距离现在时间有多少小时
    select timestampdiff(hour,'2015-4-28 19:12:00',now());

    -- date-format 日期格式化
    select date_format(now(),'%Y年%m月%d日 %h:%i:%s');

    -- inet_aton将ip转换为整型数字
    select inet_aton('192.168.1.2');

    -- inet_ntoa将数字转换成ip
    select inet_ntoa(3232235778);

    -- unix-timestamp将系统的日期转为数字
    select unix_timestamp(now());

    -- from_unixtime将系统的日期从数字转换为正常的日期
    select from_unixtime(1430308974);

    -- date_format使用日期格式化让日期整数改成正常的日期
    select date_format(from_unixtime(1430308974),'%Y年%m月%d日');

    -- concat-ws 字符串连接  用-将hello和china连接起来 
    select concat_ws('-','hello','china');

    -- concat()在学生名字前面添加姓名:
    select concat('姓名:',sname)from student;

    -- 全部改为小写lower
    select lower('HEEllo');

    -- 全部改为大写  upper
    select upper('heellow');

    -- 从左边截取2个字 left
    select left('中国人',2);

    -- 从右边截取2个字right
    select right('中国人',2);
    -- concat()把张三丰左边的张字提取出来,把数据修改成张老师
    select concat(left('张三丰',1),'老师');

    -- mid()截取从2开始截取一个,此处为从1开始
    select mid('hello',2,1);

    -- mid从3开始截取2个
    select mid('hello',3,2);

    -- ifnull  判断是否为空,如果第一个值为空,就显示后面的值,如果不为空就显示前面的值
    select ifnull(null,'ok');

    select ifnull('abc','ok');

    -- 格式化数字增加2位00的小数位
    select  format(100,2);

    -- passwored返回41位密文字符串
    select password('连少蕊');
    -- md5返回32位密文字符串
    select md5('张三');
    -- sha1返回40位密文字符串
    select sha1('连少蕊');
    --   if()判断,一般后面有三个值,一个为条件2个值a默认为假,null,''也是假
    select if('a','b','c'),if('','r','u'),if(null,'a','f');
    -- ifnull 如果第一个值为空的就显示缺考,如果不为空就显示前面的
    select sname,ifnull(sscore, '缺考') 缺考 from student;

    select sid,sname,sgender,sdept,ifnull(stid,'无代课老师') 无代课老师 from student;

    -- 删除stid=1 的学生
    delete from student where stid=1;







  • 相关阅读:
    java xpath
    structs2 Interceptor
    jetbrains idea web help
    Java xpath example code THE RIGHT WAY
    java xpath engine
    本地安装jruby到maven仓库,由于公司网络不给力
    skybe的linux下的版本下载
    mybastis学 rails有migration了
    Using Java from Ruby with JRuby IRB
    PropertyPlaceholderConfigurer
  • 原文地址:https://www.cnblogs.com/lsr111/p/4467530.html
Copyright © 2020-2023  润新知