zoukankan      html  css  js  c++  java
  • mysql 使用技巧 where条件连接;inner join内连接;外连接(left outer join,right outer join)

    mysql的inner join等价于where条件连接查询

    内连接 

    inner join 省略形式  join

    外连接

    左连接 left outer join 省略形式 left join 

    右连接 right outer join 省略形式 right join  

    两张表内容:

    mysql> use RUNOOB;
    Database changed
    mysql> SELECT * FROM tcount_tbl;
    +---------------+--------------+
    | runoob_author | runoob_count |
    +---------------+--------------+
    | 菜鸟教程  | 10           |
    | RUNOOB.COM    | 20           |
    | Google        | 22           |
    +---------------+--------------+
    3 rows in set (0.01 sec)
     
    mysql> SELECT * from runoob_tbl;
    +-----------+---------------+---------------+-----------------+
    | runoob_id | runoob_title  | runoob_author | submission_date |
    +-----------+---------------+---------------+-----------------+
    | 1         | 学习 PHP    | 菜鸟教程  | 2017-04-12      |
    | 2         | 学习 MySQL  | 菜鸟教程  | 2017-04-12      |
    | 3         | 学习 Java   | RUNOOB.COM    | 2015-05-01      |
    | 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
    | 5         | 学习 C      | FK            | 2017-04-05      |
    +-----------+---------------+---------------+-----------------+
    5 rows in set (0.01 sec)

    接下来我们就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样)来连接以上两张表来读取runoob_tbl表中所有runoob_author字段在tcount_tbl表对应的runoob_count字段值:

    inner join

    mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
    +-------------+-----------------+----------------+
    | a.runoob_id | a.runoob_author | b.runoob_count |
    +-------------+-----------------+----------------+
    | 1           | 菜鸟教程    | 10             |
    | 2           | 菜鸟教程    | 10             |
    | 3           | RUNOOB.COM      | 20             |
    | 4           | RUNOOB.COM      | 20             |
    +-------------+-----------------+----------------+
    4 rows in set (0.00 sec)

    等价于where的条件查询

    mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;
    +-------------+-----------------+----------------+
    | a.runoob_id | a.runoob_author | b.runoob_count |
    +-------------+-----------------+----------------+
    | 1           | 菜鸟教程    | 10             |
    | 2           | 菜鸟教程    | 10             |
    | 3           | RUNOOB.COM      | 20             |
    | 4           | RUNOOB.COM      | 20             |
    +-------------+-----------------+----------------+
    4 rows in set (0.01 sec)

  • 相关阅读:
    Python爬取+BI分析后,微博求助患者的眼泪,全被数据看见了
    BZOJ4321 queue2
    BZOJ4321 queue2
    BZOJ4321 queue2
    Oracle字段根据逗号分割查询数据
    Oracle字段根据逗号分割查询数据
    Oracle字段根据逗号分割查询数据
    Oracle字段根据逗号分割查询数据
    基于Web实现在线绘画拓扑图[GraphEditor]
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/InternetJava/p/12726916.html
Copyright © 2011-2022 走看看