zoukankan      html  css  js  c++  java
  • 多表查询

    连表查询

      总是在连接的时候创建一张大表,里面存放的是两张表的笛卡尔积

        在根据条件进行筛选就可以了 

         select * from department,employee where department.id = employee.dep_id;  当有相同的字段名时候需要制定表名。

         select * from department,employee where id = employee.dep_id;    

      连接方式:

            select    *    from   表1,表2    where    条件;

            内连接:inner    join .......on.........    后面可以直接跟条件语句

                select    *    from    表1   Inner    join   表2     on    条件;      select * from department inner join employee on department.id=dep_id;      select * from department as t1 inner join employee as t2 on t1.id=t2.dep_id;

    子查询  

            外连接:

                左连接     left    join    ...... on......

                      select    *    from    表1   left   join   标2    on     条件;select * from department as t1 left join employee on t1.id = dep_id;     左表的数据都会保留

                右连接     right    join   ......on.......

                      select    *    from    表1   right     join   标2    on     条件;

                        select * from department as t1 right join employee on t1.id=dep_id;

                全外连接       union

                    select * from department as t1 left join employee on t1.id = dep_id;    union    select * from department as t1 right join employee on t1.id=dep_id;

    1.找到技术部的所有人的姓名   

     select t1.name as department,group_concat(t2.name) from department as t1 left join employee as t2 on t1.id=t2.dep_id where t1.name='技术';

    2.找到人力资源部的年龄大于40岁的人的姓名。

    select t1.name as department,group_concat(t2.name) from department as t1 left join employee as t2 on t1.id=t2.dep_id where t1.name='人力资源' and t2.age>40;

  • 相关阅读:
    处理在finally中出现的异常(Java)
    【转】alt和title属性的区别及应用
    IE6下兼容CSS属性minheight的解决办法
    javascript中判断字符串是否以指定字符串开始或结尾
    IE6兼容改造中的反思
    字符操作函数
    魔术公式
    抽象类和纯虚函数
    悬空指针
    重载
  • 原文地址:https://www.cnblogs.com/ch2020/p/12909849.html
Copyright © 2011-2022 走看看