Home >Database >Mysql Tutorial >What are the sql statements in MySQL?

What are the sql statements in MySQL?

WBOY
WBOYforward
2023-05-30 17:04:511640browse

    The following two tables are explained

    What are the sql statements in MySQL?

    SELECT

    SELECT "字段" FROM "表名";
    SELECT Store_Name FROM Store_Info;
    显示表格中的一个或数个字段的所有数据记录

    What are the sql statements in MySQL?

    DISTINCT

    SELECT DISTINCT "字段" FROM "表名";
    select distinct store_name from store_info;
    不显示重复的数据记录

    What are the sql statements in MySQL?

    WHERE

    SELECT "字段" FROM "表名" WHERE "条件";
    查找store_info 表中 符号sales=700条件 的 store_name字段
    有条件查询

    What are the sql statements in MySQL?

    ##AND OR

    SELECT "字段" FROM "表名" WHERE "条件1" 
    select store_name,sales,date from store_info where sales > 1000 or (sales < 500 and sales > 200);
    且 或

    IN

    SELECT "字段" FROM "表名" WHERE "字段" BETWEEN (&#39;值1&#39; AND &#39;值2&#39;,...);
    select * from store_info where store_name in (&#39;Houston&#39;,&#39;Los Angeles&#39;);
    显示已知的值的数据记录

    What are the sql statements in MySQL?

    BETWEEN

    SELECT "字段" FROM "表名" WHERE "字段" BETWEEN &#39;值1&#39; AND &#39;值2&#39;;
    select * from store_info where sales between 200 and 500;
    显示两个值范围

    What are the sql statements in MySQL?

    Wildcard character

    %; percent sign means zero or one Or multiple characters

    _: Underscore represents a single character

    'A_Z': All strings starting with ‘A', another character of any value, and ending with ‘Z. For example, 'ABZ' and 'A2Z' both fit this pattern, but 'AKKZ' does not (because there are two characters between A and Z, not one).

    'ABC%': All strings starting with 'ABC’. For example, 'ABCD' and 'ABCABC' both fit this pattern.

    '%XYZ': All strings ending with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' both fit this pattern.
    '%AN%': All strings containing the pattern 'AN'. For example, 'LOS ANGELES' and 'SAN FRANCISCo' both fit this pattern.
    '_AN%': All strings whose second letter is 'A′ and whose third letter is 'N’. For example, 'SAN FRANCIScO' fits this pattern, while 'LoS ANGELES' does not.

    LIke

    SELECT "字段" FROM "表名" WHERE"字段" LIKE {模式};
    select * from store_info where store_name like &#39;%os%&#39;;
    匹配一个模式来找出我们要的数据记录

    What are the sql statements in MySQL?

    ORDER BY

    SELECT "字段" FROM "表名" [WHERE "条件"] ORDER BY "字段" [ASC,DESC];
    select * from store_info order by sales;
    select * from store_info order by sales desc;
    #ASC 是按照升序进行排序的,是默认的排序方式。
    #DESC 是按降序方式进行排序

    What are the sql statements in MySQL?

    Function

    Mathematical function;

    abs (x)                           返回x的绝对值
    
    rand ()                           返回o到1的随机数
    
    mod (x,y)                         返回x除以y以后的余数
    
    power (x,y)                       返回x的y次方
    
    round (x)                         返回离x最近的整数
    
    round (x,y)                       保留x的y位小数四舍五入后的值
    
    sqrt(x)                           返回x的平方根
    
    truncate (x, y)                   返回数字x截断为y位小数的值
    
    ceil(x)                           返回大于或等于x的最小整数
    
    floor(x)                          返回小于或等于x的最大整数
    
    greatest (x1,x2...)               返回集合中最大的值
    
    least(x1,x2...)                   返回集合中最小的值
    
    聚合函数:
    avg ()                            返回指定列的平均值
    
    count ()                          返回指定列中非NULL值的个数
    
    min ()                            返回指定列的最小值
    
    max ()                            返回指定列的最大值
    
    sum (x)                           返回指定列的所有值之和

    What are the sql statements in MySQL?##city table

    #count(*)包括了所有的列的行数,在统计结果的时候,不会忽略列值为NULL
    #count(列名)只包括列名那一列的行数,在统计结果的时候,会忽略列值为NULL的行
    What are the sql statements in MySQL?String function

    trim ()						返回去除指定格式的值
    
    concat (x, y)				将提供的参数x和y拼接成一个字符串
    
    substr(x,y)					获取从字符串x中的第y个位置开始的字符串,跟		  		   									substring()函数作用相同
    
    substr(x,y,z)				获取从字符串x中的第y个位置开始长度为z的字符串
    
    length (x)					返回字符串x的长度
    
    replace (x,y,z)				将字符串z替代字符串x中的字符串y
    
    upper (x)					将字符串x的所有字母变成大写字母
    
    lower (x)					将字符串x的所有字母变成小写字母
    
    left (x, y)					返回字符串x的前y个字符
    
    right (x, y)				返回字符串 x的后y个字符
    
    repeat (x,y)				将字符串x重复y次
    
    space (x)					返回x个空格
    
    strcmp(x,y)					比较x和y,返回的值可以为-1,0,1
    
    reverse (x)					将字符串x反转

    Commonly used functions Example:

    concat

    ##If sql_mode turns on PIPES_AS_CONCAT,’||’ is regarded as a string concatenation operator instead of an OR operator. It is similar to the string splicing function Concat. It is similar to Oracle. It is the same as the Oracle database.What are the sql statements in MySQL?

    substrWhat are the sql statements in MySQL?

    trimWhat are the sql statements in MySQL?

    regionWhat are the sql statements in MySQL?

    ##replace

    What are the sql statements in MySQL?

    group by

    select "字段1",sum("字段2") from "表名" group by "字段1";
    group by 有一个原则,就是select后面的所有列中,没有使用聚合函数的列,必须出现在group by后面
    What are the sql statements in MySQL?

    having

    What are the sql statements in MySQL?

    Used to get the record set returned by the group by statement, usually used in conjunction with the group by statement

    The existence of the having statement makes up for the shortcoming that the where keyword cannot be used in conjunction with aggregate functions. If only the function column is selected, then there is no need for the group by clause


    Alias

    select "表格别名"."字段一" [AS] "字段别名" from "表格名" [AS] "表格别名";
    字段别名,表格别名
    What are the sql statements in MySQL?

    Subquery

    select "字段1" from "表格" where "字段2" [比较运算符]     #外查询
    select "字段1" from "表格2" where "条件";
    连接表格,在where 子句或 having 子句中插入另一个 sql语句
    可以是符号的运算符,例如 = > < <= >=  也可以文字的运算符 例如 LIKE IN BETWEEN
    What are the sql statements in MySQL?

    exists

    用来测试内查询有没有产生任何结果,类似布尔值是否为真
    #如果有的话,系统就会执行外查询中的SQL语句,若是没有的话,那整个SQL语句就不会产生任何结果。
    语法:SELECT "字段1" from "表格1" where exists (SELECT * FROM "表格2" WHERE "条件");
    SELECT SUM(Sales) FROM Store_Info WHERE EXISTS (SELECT * FROM location WHERE Region = &#39;West&#39;);
    What are the sql statements in MySQL?

    Table link

    What are the sql statements in MySQL?Experiment with the following two tables

    inner join(内连接): 只返回两个表中联结字段相等的行
    left join(左连接):返回包括左表中的所有记录和右表中联结字段相等的记录
    right join (右连接):返回包括右表中的所有记录和左表中联结字段相等的记录

    What are the sql statements in MySQL?

    使用子查询实现多表查询

    What are the sql statements in MySQL?

    create view

    视图,可以当作是虚拟表或存储查询
    视图跟表格的不同是,表格中有实际储存数据记录,而视图是建立在表格之上的一个架构,它本身并不实际储存数据记录。临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
    视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。
    视图表不会像临时表一样,断开连接后自动消失
    视图表保存的是select 语句查询的结果,它本身不存储数据
    当视图表的结构跟存储数据的原始表结构相同则可以修改或插入数据,如果不一样则不能修改或插入数据(比如多表连接查询的结果)

    What are the sql statements in MySQL?

    union

    union:生成结果的数据记录值将没有重复,且按照字段的顺序进行排序
    语法:[select 语句1] union [select 语句2];

    union all:将生成结果的数据记录值都列出来,无论有多少重复
    语法:[select 语句1] union all [select 语句2];

    What are the sql statements in MySQL?

    What are the sql statements in MySQL?

    交集值

    取两个SQL语句结果的交集
    select A.store_name from location A inner join store_info B on A.store_name=B.store_name;
    
    select A.store_name from location A inner join store_info B using(store_name);

    What are the sql statements in MySQL?

    两表其中的一个表没有指定的行,而另一个表这个行有重复不适用,要求两个表确实有交集的行的时候用

    What are the sql statements in MySQL?

    取两个SQL语句结果的交集,且没有重复

    What are the sql statements in MySQL?

    无交集值

    显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复

    What are the sql statements in MySQL?

    case

    是SQL用来做为 if-then-else 之类逻辑的关键字
    语法:select casd("字段名")
           when "条件1" then "结果2"
           when "条件2" then "结果2"
           ....
           [else "结果N"]
           end
    from "表名";
    
    #条件可以是一个数值或公式。else 子句并不是必须的

    What are the sql statements in MySQL?

    空值(null) 和无值(’ ') 的区别

    1.无值的长度为0,不占用空间的;而 NULL值的长度是NULL,是占用空间的。
    2.IS NULL或者 IS NoT NULL,是用来判断字段是不是为NULL或者不是 NULL,
                                                不能查出是不是无值的。
    3.无值的判断使用=''或者''来处理。代表不等于。
    4.在通过 count()指定字段统计有多少行数时,如果遇到NULL值会自动忽略掉,遇到无值会加入到记录中进行计算。

    What are the sql statements in MySQL?

    What are the sql statements in MySQL?

    What are the sql statements in MySQL?

    What are the sql statements in MySQL?

    正则表达式 

    匹配模式 描述 实例
    ^ 匹配文本的开始字符 ‘^bd’ 匹配以bd开头的字符串
    $ 匹配文本的结束字符 ‘qn$’ 匹配以qn结尾的字符串
    . 匹配任何单个字符 's.t’匹配任何s和t之间有一个字符的字符串
    * 匹配零个或多个在它前面的字符 ‘fo*t’ 匹配t前面有任意个o
    + 匹配前面的字符1次或多次 ‘hom+’ 匹配ho开头,后面至少一个m的字符串
    字符串 匹配包含指定的字符串 ‘clo’ 匹配含有clo的字符串
    p1|p2 匹配p1或p2 ‘bg | fg’ 匹配bg或fg
    […] 匹配字符集合中的任意一个字符 ‘[abc]’ 匹配a或b或c
    [^…] 匹配不在括号中的任何字符 ‘[ ^ ab]’ 匹配不含a或b的字符串
    {n} 匹配前面的字符串n次 ‘g{2}’ 匹配含有2个g的字符串
    {n,m} 匹配前面的字符串至少n次,至多m次 ‘f{1,3}’ 匹配f最少1次 最多3次

    语法:select "字段" from "表名" where "字段" regexp {模式};

    What are the sql statements in MySQL?

    What are the sql statements in MySQL?

    存储过程

    存储过程是一组为了完成特定功能的SQL语句集合。
    存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来,这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时,只需要调用它即可。存储过程在执行上比传统SQL速度更快、执行效率更高。

    存储过程的优点:
    1、执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
    2、SQL语句加上控制语句的集合,灵活性高
    3、在服务器端存储,客户端调用时,降低网络负载
    4、可多次重复被调用,可随时修改,不影响客户端调用
    5、可完成所有的数据库操作,也可控制数据库的信息访问权限

    创建存储过程

    delimiter $$			##将语句的结束符号从分号;临时改为两个$$ 可以自定义
    create procedure proc() ##创建存储过程,过程名为proc,不带参数
               
    -> begin                ##过程体以关键字 begin 开始
    
    -> select * from Store_info; ##过程体语句
    -> end $$                    ##过程体以关键字 end结束
    delimiter;					 ##将语句的结束符号恢复为分号
    
    call proc					##调用存储过程
    
    show create procedure [数据库.]存储过程名;

    What are the sql statements in MySQL?

    存储过程的参数

    in 输入参数:表示调用折向过程传入值(传入值可以是字面量或变量)
    out 输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
    inout 输入输出参数:即表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)

    What are the sql statements in MySQL?

    查看存储过程

    show create procedure [数据库.]存储过程名;

    删除存储过程

    存储过程内容的修改方法是通过删除原有存储过程,之后再以相同的名词创建新的存储过程
    drop procedure if exists Proc;
    #仅当存在时删除,不添加 if exists时,如果指定的过程不存在,则产生一个错误
     drop procedure proc1;

    存储过程的控制语句

    (1) 条件语句 if-then-else …end if

     delimiter $$
     create procedure proc2(in num int) 
     begin
     declare var int;
     set var=num*2;
     if var >= 10 then 
     update t set id=id+1; 
     else
     update t set id=id-1; 
     end if; 
     end$$

    What are the sql statements in MySQL?

    (2)循环语句while… end while

    What are the sql statements in MySQL?

    The above is the detailed content of What are the sql statements in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete