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;

  • 相关阅读:
    什么是ETL?5分钟看完秒懂
    横向滚动 css
    解决echarts中横坐标值显示不全(自动隐藏)问题
    Echarts
    post 二进制流下载文件
    如何停止foreach
    日期格式 js
    cookie 属性
    HTML5 file对象和blob对象的互相转换
    前端图片压缩
  • 原文地址:https://www.cnblogs.com/ch2020/p/12909849.html
Copyright © 2011-2022 走看看