zoukankan      html  css  js  c++  java
  • 基于mysql的一些sql语法

    Sql:

    distinct:

    select distinct * from tea; 

    Tea中每行的数据必有不同,若有两行相同的,则只输出一行

    Limit:

    select * from stu limit 1,3

    从stu表的第二行开始,选取3行

    Null:

    select * from stu where name is null;

    查询name为空的数据

    Regexp:

     select * from stu where name regexp '.';

    匹配正则表达式

    ^[0-9]:

    表示匹配从0-9数字开始的字符串

    [^0-9]:

    表示匹配不含0-9数字的字符串

    [0-9]:

    表示匹配含有0-9数字的字符串

    [0-9]$:

    表示匹配以0-9数字结束的字符串

    Concat:

    select concat(name,'(',id,')') from stu

    将数据组装成name(id)形式列出

    Trim:

    select concat(trim(name),'(',id,')') from stu;

    Name中的数据去除头尾连续的空格

    Ltrim:

    select concat(ltrim(name),'(',id,')') from stu;

    去除name中开始时连续的空格

    Rtirm:

    select concat(rtrim(name),'(',id,')') from stu;

    去除name中结束时连续的空格

    now():

    select now();

    返回当前时间

    自然连接:

    两个表中都有例如name属性的化,只显示一个

    Union:

    select * from stu where name = 'dj' union all select * from stu where name = 'dd';

    合并两个查询的数据集,union不显示重复的数据,union all显示重复的数据

    全文本搜索:

    创建表时需:

    指明engine引擎为myisam,指明检索的属性note_text

    通过match和against进行搜索匹配

    和like相比,这个会返回一定的顺序,匹配频率等

     

    必须声明在 boolean mode下

    优先级:

    insert low_priority into stu values(null,'d');

    · LOW_PRIORITY关键字应用于DELETE、INSERT、LOAD DATA、REPLACE和UPDATE。

    · HIGH_PRIORITY关键字应用于SELECT和INSERT语句。

    · DELAYED关键字应用于INSERT和REPLACE语句。

    LOW_PRIORITY :低优先级。如果您使用LOW_PRIORITY关键词,则INSERT的执行被延迟,直到没有其它客户端从表中读取为止。在读取量很大的情况下,发出INSERT LOW_PRIORITY语句的客户端有可能需要等待很长一段时间(甚至是永远等待下去)。

    DELAYED :延迟。如果您使用DELAYED关键字,则服务器会把待插入的行放到一个缓冲器中,而发送INSERT DELAYED语句的客户端会继续运行。如果表正在被使用,则服务器会保留这些行。当表空闲时,服务器开始插入行,并定期检查是否有新的读取请求。如果有新的读取请求,则被延迟的行被延缓执行,直到表再次空闲时为止。

    注意,目前在队列中的各行只保存在存储器中,直到它们被插入到表中为止。这意味着,如果您强行中止了mysqld(例如,使用kill -9)或者如果mysqld意外停止,则所有没有被写入磁盘的行都会丢失。

    HIGH_PRIORITY:高优先级。如果您指定了HIGH_PRIORITY,同时服务器采用--low-priority-updates选项启动,则HIGH_PRIORITY将覆盖--low-priority-updates选项。

    --low-priority-updates:mysqld命令行选项。set low_priority_updates = 1 指定更新使用低优先级(默认更新优先级高于查询)。

    Ignore:

    update ignore stu set name = 'dk' where id = '13';

    即使发生错误,也要继续更新

    truncate table:

    删除并创建表,更快的用于删除表中所有的数据

     select last_insert_id();

    得到最后一次插入的id

     

    一个引擎的表不能引用另个引擎的表的外键

    alter table:

    更新表定义

    更新视图(创建或者替代视图)

    create or replace view s as select * from stu where id%2 = 0;

    delimiter //

    将结束符号改成 //

    存储过程:

    mysql> create procedure stuName()

        -> begin 

        -> select name

        -> from stu;

        -> end //

    call stuName()//

    调用存储过程stuName

    drop procedure if exists stuName//

    如果stuname存在,则删除存储过程stuName

    create procedure stuFun( out a decimal(8,2), out b decimal(8,2) ) begin select count(*) into a from stu; select avg(id) into b from stu; end//

    将count(*)值传给a , 将avg(id)值传给b, decimal(8,2) 有效位8位,保留2位小数

    call stuFun(@a,@b)//

    调用stuFun这个存储过程,将count(*)值传给a , 将avg(id)值传给b

    select @a//

    显示a的结果,也就是count(*)的结果

    create procedure stuSum( in num int, out sum decimal(8,2) ) begin select count(*) from stu where id = num into sum; end

    传入num值,得到sum值

    call stuSum(1,@sum)//

    调用存储过程

    Select @sum;

    显示sum的结果

    create procedure stuPrint( in num int ) begin select name from stu where id = num; end//

    传入num值,根据num值来获得用户名

    create procedure fun( 

    in num int, i

    n b boolean, 

    out result decimal(8,2) 

    begin 

    declare total decimal(8,2) ; 

    declare xishu int default 2;

    select count(*) from stu where id = num 

    into total; 

    if b then 

    select total*xishu into total; 

    end if; 

    select total into result;

     end//

    declare声明变量

    if b then … end fi; 如果b为真则。。

    触发器:

    当执行update, delete ,insert时触发一条sql语句

    创建用户:

     create user dj identified by '123456';

    重命名用户:

     rename user dj to dd;

    删除用户:

    drop user dj;

    给用户权限:

    grant select on test.* to dj;

    让dj用户可以对test数据库所有的表进行查询

    修改用户密码:

    set password for dj = password('123');

    修改自己权限密码

    set password = password('123');

     

  • 相关阅读:
    mplayerww-34106 gcc-4.5.1
    再更新ww的mingw MinGW-full-20101119
    mplayer-ww-37356 compile with mingw gcc 4.5.1 修复无法播放wmv
    CodeBlocks_20160621_rev10868_gcc5.3.0
    更新ww的mingw MinGW-full-20101119
    HTML5学习笔记(六)web worker
    HTML5学习笔记(五)存储
    HTML5学习笔记(四)语义元素
    HTML5学习笔记(三)新属性、功能
    HTML5学习笔记(二)新元素和功能
  • 原文地址:https://www.cnblogs.com/dj3839/p/7099079.html
Copyright © 2011-2022 走看看