zoukankan      html  css  js  c++  java
  • JOIN的区别

    CREATE TABLE `j1` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `c1` varchar(20) NOT NULL DEFAULT '',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;

    CREATE TABLE `j2` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `c1` varchar(20) NOT NULL DEFAULT '',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;

    mysql> select * from j1;
    +----+-------+
    | id | c1    |
    +----+-------+
    |  1 | user1 |
    |  2 | user2 |
    |  3 | user3 |
    |  4 | user4 |
    |  5 | user5 |
    +----+-------+
    5 rows in set (0.01 sec)

    mysql> select * from j2;
    +----+--------------+
    | id | c1           |
    +----+--------------+
    |  1 | user_detail1 |
    |  3 | user_detail3 |
    |  4 | user_detail4 |
    |  6 | user_detail6 |
    |  8 | user_detail8 |
    +----+--------------+
    5 rows in set (0.00 sec)

     mysql> select * from j1 left join j2 on j1.id = j2.id;
    +----+-------+------+--------------+
    | id | c1    | id   | c1           |
    +----+-------+------+--------------+
    |  1 | user1 |    1 | user_detail1 |
    |  2 | user2 | NULL | NULL         |
    |  3 | user3 |    3 | user_detail3 |
    |  4 | user4 |    4 | user_detail4 |
    |  5 | user5 | NULL | NULL         |
    +----+-------+------+--------------+
    5 rows in set (0.00 sec)

    mysql> select * from j1 right join j2 on j1.id = j2.id;
    +------+-------+----+--------------+
    | id   | c1    | id | c1           |
    +------+-------+----+--------------+
    |    1 | user1 |  1 | user_detail1 |
    |    3 | user3 |  3 | user_detail3 |
    |    4 | user4 |  4 | user_detail4 |
    | NULL | NULL  |  6 | user_detail6 |
    | NULL | NULL  |  8 | user_detail8 |
    +------+-------+----+--------------+
    5 rows in set (0.00 sec)

     mysql> select * from j1 inner join j2 on j1.id = j2.id;
    +----+-------+----+--------------+
    | id | c1    | id | c1           |
    +----+-------+----+--------------+
    |  1 | user1 |  1 | user_detail1 |
    |  3 | user3 |  3 | user_detail3 |
    |  4 | user4 |  4 | user_detail4 |
    +----+-------+----+--------------+
    3 rows in set (0.00 sec)

    mysql> desc select * from j1 left join j2 on j1.id = j2.id where j2.id is not null;
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    | id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra       |
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    |  1 | SIMPLE      | j2    | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |    5 |    80.00 | Using where |
    |  1 | SIMPLE      | j1    | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | mytest.j2.id |    1 |   100.00 | NULL        |
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    2 rows in set, 1 warning (0.00 sec)

    mysql> desc select * from j2 left join j1 on j1.id = j2.id where j1.id is not null;
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    | id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref          | rows | filtered | Extra       |
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    |  1 | SIMPLE      | j1    | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL         |    5 |    80.00 | Using where |
    |  1 | SIMPLE      | j2    | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | mytest.j1.id |    1 |   100.00 | NULL        |
    +----+-------------+-------+------------+--------+---------------+---------+---------+--------------+------+----------+-------------+
    2 rows in set, 1 warning (0.00 sec)
  • 相关阅读:
    android prgoressBar setProgressDrawable 在4.0系统式正常,在2.3系统上不能正常使用的问题
    android 动态控制状态栏显示和隐藏
    静态成员变量和静态成员函数(static)
    WPF中如何使用BusyIndicator
    C# 面试题 二
    C#面试题及答案 一 <转来的,貌似有看评论说有错误,正在一个个纠正中…… 也望园友们指出>
    [转载]如何用Visual Studio制作安装包
    WPF九宫格图片自定义皮肤(新博速读2.0)
    附加题
    附加题 回答问题
  • 原文地址:https://www.cnblogs.com/allenhu320/p/11557956.html
Copyright © 2011-2022 走看看