zoukankan      html  css  js  c++  java
  • mysql innerjoin,leftjoin,group by,having

    1, cross join 交叉连接

    select t1.*,t2.* from table1 t1,table2 t2;
    select t1.*,t2.* from table1 t1 cross join table2 t2;
    我们把上述"没有任何限制条件的连接方式"称之为"交叉连接""交叉连接"后得到的结果跟线性代数中的"笛卡尔乘积"一样。

    2, inner join 内链接

    select t1.*,t2.* from table1 t1,table2 t2 where t1.id = t2.id;
    select t1.*,t2.* from table1 t1 inner join table2 t2 on t1.id = t2.id;
    "有条件的" 交叉链接

    3,左外链接 left join

    select t1.*,t2.* from table1 t1 left join table2 t2 on t1.id = t2.id;
    如果连接字段右表中不唯一,左外链接会出现重复列情况, 
    所以一般用主键进行关联,注意主表的选择;
    mysql> select * from stu;
    +----+-------+
    | id | name  |
    +----+-------+
    |  1 | Kobe  |
    |  2 | James |
    |  3 | Bosh  |
    +----+-------+
    3 rows in set (0.03 sec)
    
    mysql> select * from class;
    +----+------+------+
    | id | s_id | c_id |
    +----+------+------+
    |  1 |    1 |    1 |
    |  2 |    1 |    2 |
    |  3 |    2 |    5 |
    +----+------+------+
    3 rows in set (0.01 sec)
    
    mysql> select * from stu left join class on stu.id= class.s_id;
    +----+-------+------+------+------+
    | id | name  | id   | s_id | c_id |
    +----+-------+------+------+------+
    |  1 | Kobe  |    1 |    1 |    1 |
    |  1 | Kobe  |    2 |    1 |    2 |
    |  2 | James |    3 |    2 |    5 |
    |  3 | Bosh  | NULL | NULL | NULL |
    +----+-------+------+------+------+
    4 rows in set (0.03 sec)

     4,group by 字段

    1,GROUP BY 语句根据一个或多个列对结果集进行分组
    2,最终查询出的结果只会显示组中一条记录
    3,
    在mysql中没有强调select指定的字段必须属于group by后的条件(和 oracle 中不同)
    mysql> select class.*,count(*) from class where id >0 group by s_id having count(*) >1;
    +----+------+------+----------+
    | id | s_id | c_id | count(*) |
    +----+------+------+----------+
    | 1 | 1 | 1 | 2 |
    | 4 | 4 | 4 | 2 |
    +----+------+------+----------+
    2 rows in set (0.02 sec)
    
    mysql> select class.* from class ;
    +----+------+------+
    | id | s_id | c_id |
    +----+------+------+
    | 1 | 1 | 1 |
    | 2 | 1 | 2 |
    | 3 | 2 | 5 |
    | 4 | 4 | 4 |
    | 5 | 4 | 7 |
    +----+------+------+
    5 rows in set (0.02 sec)

    5,having 

    1,已经筛选出的字段,having和where的效果是等效2,未筛出的字段 过滤只能用where
    3,group by 的组内过滤, 只能用having [having 的字段必须是聚合函数或者出现在select检索中出现]
    mysql> select id,s_id from class having s_id >2; 
    +----+------+
    | id | s_id |
    +----+------+
    |  4 |    4 |
    |  5 |    4 |
    +----+------+
    2 rows in set (0.02 sec)
     
    mysql> select id from class having s_id >2; 
    1054 - Unknown column 's_id' in 'having clause'
    多想,多试
  • 相关阅读:
    软件过程的守护神
    C++一个简单的手柄类模板
    矿Spring入门Demo
    [Docker] Prune Old Unused Docker Containers and Images
    [Ramda] Compose lenses
    [Docker] Build Your Own Custom Docker Image
    [SCSS] Reuse Styles with the SCSS @mixin Directive
    [Angular] Router outlet events
    [Angular] Subscribing to router events
    [Angular] Enable router tracing
  • 原文地址:https://www.cnblogs.com/junyi0120/p/11734552.html
Copyright © 2011-2022 走看看