• 20141104--SQL连接查询,联合查询


     1 ---------------------------连接查询--------------------------------
     2 --横向连接查询
     3 --可以将子查询放在from之前,用来替换显示出来的信息
     4 select name as 名字,sex,age,(select name from bumen where bumen.code=biao2.bumen)部门,(select ceo from bumen where bumen.code=biao2.bumen)as ceo from biao2
     5 --连接查询 两个表必须有联系。
     6 --如果两个表有相同的列名,则需要明确是哪个表的列,格式:表名.列名
     7 --连接查询较为简便的方法,
     8 select biao2.name,sex,age,bumen.name,ceo from biao2,bumen where biao2.bumen=bumen.code
     9 --join on 另一种写法 join(加入) on(在..上) 格式:from 表名1 join 表名2 on 两个表的列关系
    10 select biao2.name,sex,age,bumen.name,ceo from biao2 join bumen on biao2.bumen=bumen.code
    11 --用 join on 连接两个表的中间三列(biao2的name,sex,age 和 bumen的name,zhineng,ceo)
    12 select biao2.name,sex,age,bumen.name,zhineng,ceo from biao2 
    13 join bumen on biao2.bumen=bumen.code 
    14 ----------join on 之前的三个关键词-------------
    15 --如果两个表没有主外键关系,不在join前加 关键词 的时候只显示两个表有联系的信息
    16 --full
    17 --在join前加 full 可以显示两个表所有的信息(包括两个表中没有联系的信息)
    18 select biao2.name,sex,age,bumen.name,zhineng,ceo from biao2 
    19 full join bumen on biao2.bumen=bumen.code 
    20 --left 
    21 --在join前加 left 只显示左边表的全部信息(包括没有关系的信息)并按左边表排序。
    22 select biao2.name,sex,age,bumen.name,zhineng,ceo from biao2 
    23 left join bumen on biao2.bumen=bumen.code 
    24 --right 
    25 --在join前加 right 只显示右边表的全部信息(包括没有关系的信息)并按右边表排序。
    26 select biao2.name,sex,age,bumen.name,zhineng,ceo from biao2 
    27 right join bumen on biao2.bumen=bumen.code 
    28 
    29 -------------------------联合查询---------------------------
    30 --纵向联合查询,
    31 --union  两个表的列数据类型必须对应一样才可以连接起来。具备自动去重的功能
    32 --在一个表中
    33 select *from biao2 where age>=39
    34 union
    35 select *from biao2 where age<=25
    36 --在两个表中
    37 select name,bumen from biao2
    38 union
    39 select ceo,code from bumen 
  • 相关阅读:
    FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:
    redis分布式锁原理与实现
    java中如何将 string 转化成 long
    FastJson中JSONObject用法及常用方法总结
    Spring IOC 一——容器装配Bean的简单使用
    静态代理和动态代理
    Spring AOP——Spring 中面向切面编程
    什么是分布式锁?实现分布式锁的三种方式
    @Consumes @Produces的作用
    pro、pre、test、dev环境
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4073220.html
Copyright © 2020-2023  润新知