zoukankan      html  css  js  c++  java
  • 【Python】sql-内连接,左连接,右连接,union

    内连接:
    mysql> select * from book_wangjing as book_1 inner join user_wangjing as user_1 on book_1.id=user_1.id limit 2;
    +----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
    | id | book_name              | book_author | price | publish_date | id | date1               | date2      | date3               | time     |
    +----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
    |  1 | webdriver 框架实战指南 | 吴老师      |   100 | 2018-05-05   |  1 | 2018-05-05 00:08:24 | 2018-05-05 | 2018-05-05 00:08:24 | 00:08:24 |
    |  2 | how google test        | 李老师      |    99 | 2018-05-05   |  2 | 2018-05-05 00:08:54 | 2018-05-05 | 2018-05-05 00:08:54 | 00:08:54 |
    +----+------------------------+-------------+-------+--------------+----+---------------------+------------+---------------------+----------+
    2 rows in set (0.00 sec)
     
    左连接:
    mysql> select a.* from author_wangjing as a left join book_wangjing as b on a.author_name=b.book_author;
    +----+-------------+--------+
    | id | author_name | salary |
    +----+-------------+--------+
    |  1 | 吴老师      |  10000 |
    |  3 | Mr Jackson  |  15000 |
    |  2 | 张老师      |  13000 |
    +----+-------------+--------+
    3 rows in set (0.07 sec)
     
    右连接:
    mysql> select * from user_wangjing as user_1 right join book_wangjing as book_1 on user_1.id=book_1.id;
    +------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
    | id   | date1               | date2      | date3               | time     | id | book_name              | book_author | price | publish_date |
    +------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
    |    1 | 2018-05-05 00:08:24 | 2018-05-05 | 2018-05-05 00:08:24 | 00:08:24 |  1 | webdriver 框架实战指南 | 吴老师      |   100 | 2018-05-05   |
    |    2 | 2018-05-05 00:08:54 | 2018-05-05 | 2018-05-05 00:08:54 | 00:08:54 |  2 | how google test        | 李老师      |    99 | 2018-05-05   |
    |    3 | 2018-05-05 00:08:55 | 2018-05-05 | 2018-05-05 00:08:55 | 00:08:55 |  3 | unit test              | 李老师      |     1 | 2018-05-05   |
    |    4 | 2018-05-05 00:08:56 | 2018-05-05 | 2018-05-05 00:08:56 | 00:08:56 |  4 | function test          | 李老师      |    10 | 2017-11-10   |
    | NULL | NULL                | NULL       | NULL                | NULL     |  5 | function test          | Mr Jackson  |    10 | 2013-06-10   |
    +------+---------------------+------------+---------------------+----------+----+------------------------+-------------+-------+--------------+
    5 rows in set (0.07 sec)
     
    mysql> select a.* from author_wangjing as a right join book_wangjing as b on a.author_name=b.book_author;
    +------+-------------+--------+
    | id   | author_name | salary |
    +------+-------------+--------+
    |    1 | 吴老师      |  10000 |
    |    3 | Mr Jackson  |  15000 |
    | NULL | NULL        |   NULL |
    | NULL | NULL        |   NULL |
    | NULL | NULL        |   NULL |
    +------+-------------+--------+
     
    union:
    mysql> select book_author from book_wangjing union select author_name from author_wangjing;
    +-------------+
    | book_author |
    +-------------+
    | 吴老师      |
    | 李老师      |
    | Mr Jackson  |
    | 张老师      |
    +-------------+
    4 rows in set (0.06 sec)
     

    1、     内连接:结果集中出现的a.author_name和b.book_author必须同时在两个表存在

    2、     左连接:结果集中出现的a.author_name必须在左表(author_wangjing表)存在且全部显示

    3、     右连接:结果集中出现的b.book_author必须在右表(book_wangjing表)存在且全部显示

    4、 union:做合并,使用union时左表的数据列要和右表的数据列一样多

  • 相关阅读:
    通过web端启动关闭服务器程序以及检测程序运行状态
    Windows 自动监听程序,游戏服务器挂掉以后,自动监听程序将其重启起来
    自动监听程序,如果程序挂了,就重启
    删除log
    封装了一个C++类,当程序意外崩溃的时候可以生成dump文件,以便确定错误原因。
    贝塞尔曲线
    golang sql连接池 超时 数据库自动断开 ->127.0.0.1:3 306: wsarecv: An established connection was aborted by the software in your host machine.
    带控制的抢庄牛牛
    龙虎斗控制
    回归模型与房价预测
  • 原文地址:https://www.cnblogs.com/jingsheng99/p/8993311.html
Copyright © 2011-2022 走看看