zoukankan      html  css  js  c++  java
  • mysql数据库优化课程---14、常用的sql技巧

    mysql数据库优化课程---14、常用的sql技巧

    一、总结

    一句话总结:

    其实就是sql中那些函数的使用

    1、mysql中函数如何使用?

    选择字段加函数即可:select id,ucase(username),age from user;

    其实就是作用域select的选择字段

    3.转大写:
    ucase();
    例子: select id,ucase(username),age from user;

    2、sql中的时间戳函数有必要学么?

    没必要:其实没必要,因为我们时间都是存的时间戳

    所以要是要时间戳对应的日期的话,可以去php中操作

    3、mysql中如何清空表(auto_increment属性设置为1)?

    delete alter:delete from user; alter table user auto_increment=1;
    truncate:truncate user;

    1.delete
    1)delete from user;
    2)alter table user auto_increment=1;

    2.truncate
    truncate user;

    4、mysql中命令的帮助?

    命令前 ?:? create

    1.简单
    ? create

    2.更多
    ? fun%

    5、巧用RAND()提取随机行?

    order by rand limit:select * from user order by rand limit 3;

    6、mysql中如何使用正则表达式?

    regexp 正则表达式:select * from user where username regexp 'php$'; 以php结尾的数据

    1.以php结尾的数据
    select * from user where username regexp 'php$';

    2.以php结尾或以linux结尾的数据
    select * from user where username regexp 'php$' or username regexp 'linux$';

    3.查找包含php或linux或user的数据
    select * from user where username regexp 'php|linux|user';

    7、mysql中的正则表达式为什么用的少?

    like:因为我们一般是从mysql中拿数据,然后扔给php,在php中用正则表达式取数据
    正则表达式其实比like慢,一般的操作我们都可以用like实现

    二、内容在总结中

    1、相关知识

    mysql字符串函数:
    1.字符串连接
    concat();
    例子: select concat('php','linux');

    2.转小写
    lcase();
    例子: select lcase('PHP IS VERY MUCH!');

    3.转大写:
    ucase();
    例子: select id,ucase(username),age from user;

    4.长度
    length();
    例子: select length('linux');

    5.取除左边的空格
    ltrim();
    例子: select length(ltrim('   linux'));

    6.取除右边的空格
    rtrim();
    例子: select length(rtrim('linux   '));

    7.重复
    repeat();
    例子: select concat(repeat('-',20),'linux');

    8.替换
    replace();
    例子: select replace('linux and java','linux','php');

    9.截取
    substring();
    例子: select substring('/usr/local/src',6,5);

    10.空格
    space();
    例子: select concat('linux',space(20),'php');

    mysql数学函数:
    1.bin();
    十进制转2进制
    例子: select bin(10);

    2.ceiling();
    取上一个整数
    例子: select ceiling(10.5);

    3.floor();
    取下一个整数
    例子: select floor(10.5);

    4.max();
    取最大数
    例子: select max(id) from user;

    5.min();
    取最小数
    例子: select min(id) from user;

    6.sqrt();
    开平方
    例子: select sqrt(100);

    7.rand();
    求随机数
    例子: select * from user order by rand();

    mysql日期函数:
    1.curdate();
    当前日期
    例子: select curdate();

    2.curtime();
    当前时间
    例子: select curtime();

    3.now();
    当前日期和时间
    例子: select now();

    4.unix_timestamp();
    当前时间戳
    例子: select unix_timestamp();

    5.from_unixtime();
    时间戳转日期
    例子: select from_unixtime(1492176896);

    6.week(date);
    一年中的第几周
    例子: select week('2017-1-8');

    7.year(date);
    日期中的年部分
    例子: select year('2017-4-14');

    8.datediff();
    日期差值
    例子: select datediff('2017-4-14','2017-4-10');

    重排auto_increment方法:
    1.delete
    1)delete from user;
    2)alter table user auto_increment=1;

    2.truncate
    truncate user;

    mysql中命令的帮助:
    1.简单
    ? create

    2.更多
    ? fun%

    巧用RAND()提取随机行:
    select * from user order by rand limit 3;

    正则表达式的使用:
    1.以php结尾的数据
    select * from user where username regexp 'php$';

    2.以php结尾或以linux结尾的数据
    select * from user where username regexp 'php$' or username regexp 'linux$';

    3.查找包含php或linux或user的数据
    select * from user where username regexp 'php|linux|user';


     
  • 相关阅读:
    Java并发与线程同步
    ArrayList源码分析
    Lock之ReentrantLock及实现生产者消费者和死锁
    SimpleDateFormat线程不安全原因及解决方案
    JDK1.7 hashMap源码分析
    java 数据操作
    java 数据流操作
    java 基础概念
    获取class 信息 java
    Java虚拟机系列(三)---内存溢出情况及解决方法
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9837444.html
Copyright © 2011-2022 走看看