zoukankan      html  css  js  c++  java
  • mysql易混淆知识点

    1,join 和 union

    join连接属于表之间的水平操作,而union 是表之间的垂直操作。简单讲就是水平操作主要是为了获得列数据,垂直操作是为了获得行数据

    cross  join       交叉连接    ansi  89  支持              ansi  92   支持inner  join        内连接     ansi  89  支持               ansi  92   支持

    outer  join        外链接     ansi  89  未引入            ansi  92   支持

    union  

     

    select  *  from a,b where a.x=b.x

    select * from a INNER JOIN  b ON a.x=b.x

    这两个语句的结果是相同的,逻辑查询和物理查询也是相同的

    因为INNER 关键字可以省略所以下面的语句也是相同的

    Select  *  from  a  JOIN b ON a.x=b.x

    如果使用ansi 92格式,当两个表中的字段相同 如上面的a.x=b.x 可以简化成如下方式

    select * from a INNER JOIN  b ON using (x)、

     

    outer join  分两种 left outer join  和 right outer join

    也可以省略outer  关键字  

    left join 时  左边的表每行都显示,没有数据时会使用null代替

    right join 时 右边的表每行都显示,没有数据时会使用null代替

     

    INNER JOIN 时 过滤条件可以写在ON子句中,而在OUTER JOIN时有过滤条件 必须是用where,否则会得出错误的结果集

    使用union 时,两个表中的列数必须相同,列的数据类型也要相同,如果不同会进行数据类型转换

    UNION          会自动过滤重复项
    UNION all     不会去除重复项

    union 单独使用等于 union  distinct   如果要显示相同的值使用union all或者如果确定无相同值是也可以使用union all 会加快查询速度

    select  *  from  a union select  * from  b    //union基本上是做多个表搜索时,对结果集并集时使用

    2,逻辑查询中语句执行先后顺序和步骤

    select 语句的 完整执行流程一共11个步骤,如果相关步骤不存在则直接跳过 

    select  (9)   distinct <select list>

    (1)  from <left table>  

    (2)   <join type>  <right table>

    (3)    on  <join condition>

    (4)    where <where condition>

    (5)    group by <group by list >

    (6)    with  {cube |  rollup}

    (7)    having <having condition>

    (10)   order by <order by list>

    (11)   limit <limit number>

     

     

    3,Any  some all  exists  not exists

    any关键字必须与比较操作符一起使用,any的意思是

    【对于子查询返回的列中任一数值,如果比较结果是true,则返回true】

    some是any的别名

    select  *  from  t  where  x>any(select  x  from  t2)

    表示x大于子查询中任意一个值就返回true

    select  *  from  t  where  x>some(select  x  from  t2)

    上面的语句是相等的

    如果操作符变成“=”,如下

    select  *  from  t  where  x=any(select  x  from  t2)

    select  *  from  t  where  x in  (select  x  from  t2)

    这两个语句也相等

     

    all  关键字也必须同比较操作符一起使用,all的意思

    【对于子查询返回的列中的所有值,如果比较结果为true,则返回true】

    select  *  from  t  where  x>all(select  x  from  t2)

     表示x大于子查询中所有值才返回true

    当使用“<>”时

    select  *  from  t  where  x<>any(select  x  from  t2)

    select  *  from  t  where  x not in(select  x  from  t2)

    上面的语句是相等的

    exists      使用exists时无论输入子查询是否返回行,exists都不会返回unknown,unknown被认为是false

     

    not exists   

    select *  from  t  where  x='city'  and exists  (select  *  from  b  where  y>10)

    exists 只关心子查询是否有行返回,而不关心返回的是什么值

    他们和in 以及not in  的区别就在于,他们的返回值不是true就是false,

     而in 和not in 存在unknown的情况

    4,索引优点:

    1,一般是作用于where子句所给出的条件相匹配的行,一是在关联操作中与其他数据表所匹配的行。

    2,对于使用mix() 和max()函数的查询的列

    3,经常使用order by 和group by的列

    4,索引可以加快查询速度,不用扫描整个表

    索引缺点

    1,索引虽然加快查询的速度,但是会降低写入操作,比如插入,修改,删除数据

    2,索引要占据磁盘空间,索引越多占据空间越大,

    MySQL只对<,<=,=,>,>=,BETWEEN,IN,以及某些时候(以通配符%和_开头作查询时,MySQL不会使用索引)的LIKE才会使用索引

    强制使用索引
    select  * FROM oc_address FORCE INDEX (PRI) where address_id>2   //PRI 指主键索引 
    忽略索引
    select  * FROM TABLE1 IGNORE INDEX (FIELD1, FIELD2)
     

    5,插入数据时如果存在则更新,不存在则插入

    前提是更新条件的字段必须是UNIQUE索引
    INSERT INTO dz_lock (lock_name,lock_id,lock_mac,acc_id,addtime) VALUES ('$lockname','$lockid','$lockmac','$acc_id','$addtime') ON DUPLICATE KEY UPDATE lock_name='$lockname',lock_MAC='$lockmac',acc_id='$acc_id', addtime='$addtime'

     

  • 相关阅读:
    【转载】[SMS]SMS内容的7bit和UCS2编码方式简介
    【转载】两篇关于字符编码的博文
    【IRA/GSM/UCS2】the difference of IRA/GSM/UCS2 character set
    【LTE】LTE中SINR的理解
    【LTE】为什么使用SNR来表征信道质量,而并不用RSRQ?这两者的区别是什么?
    【C++】C++为什么要引入引用这个复合类型?
    【HTML55】HTML5与CSS3基础教程
    python 三种单例模式
    python3.10 新增的 match case 语句
    Python pyqt5简单的图片过滤工具
  • 原文地址:https://www.cnblogs.com/fslnet/p/4583838.html
Copyright © 2011-2022 走看看