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;

  • 相关阅读:
    数据处理之PostgreSQL过程语言学习
    Thinkphp中的assign() 和 display()
    JS截取与分割字符串常用技巧总结
    三种JS截取字符串方法
    十大经典排序算法的JS版
    js时间与毫秒互相转换
    javascript--清除表单缓存
    JS join()和split()方法、reverse() 方法、sort()方法
    JS数组去重的几种常见方法
    CSS样式大全
  • 原文地址:https://www.cnblogs.com/ch2020/p/12909849.html
Copyright © 2011-2022 走看看