zoukankan      html  css  js  c++  java
  • select * from a,b探讨

    select * from a,b探讨

    今天看同事代码里使用了select * from a,b where a.id=b.id,而我平时都是使用select * from a inner join b where a.id=b.id,于是查了下,发现:

    1)单纯的select * from a,b是笛卡尔乘积

    2)select * from a,b where a.id=b.id相当于inner join


    #### 验证

    1)创建两张表

    create table userinfo(
            uid int(10) not null default 0,
            report_id int(10) not null default 0,
            primary key(uid)
           ) engine=innodb default charset=utf8;
    
    create table report(
           report_id int(10) not null default 0,
           description varchar(255) default '',
           primary key(report_id)
           ) engine=innodb default charset=utf8;
    

    2)插入测试数据
    insert into userinfo values(1,1),(2,1),(3,2),(4,6);
    insert into report values(1,'第一条'),(2,'第二条'),(3,'第三条');
    
    mysql> select * from userinfo;
    +-----+-----------+
    | uid | report_id |
    +-----+-----------+
    |   1 |         1 |
    |   2 |         1 |
    |   3 |         2 |
    |   4 |         6 |
    +-----+-----------+
    4 rows in set (0.00 sec)
    
    mysql> select * from report;
    +-----------+-------------+
    | report_id | description |
    +-----------+-------------+
    |         1 | 第一条      |
    |         2 | 第二条      |
    |         3 | 第三条      |
    +-----------+-------------+
    3 rows in set (0.00 sec)
    

    3)验证

    单独的select * from a,b

    select * from userinfo,report
    

    结果

    mysql> select * from userinfo,report;
    +-----+-----------+-----------+-------------+
    | uid | report_id | report_id | description |
    +-----+-----------+-----------+-------------+
    |   1 |         1 |         1 | 第一条      |
    |   1 |         1 |         2 | 第二条      |
    |   1 |         1 |         3 | 第三条      |
    |   2 |         1 |         1 | 第一条      |
    |   2 |         1 |         2 | 第二条      |
    |   2 |         1 |         3 | 第三条      |
    |   3 |         2 |         1 | 第一条      |
    |   3 |         2 |         2 | 第二条      |
    |   3 |         2 |         3 | 第三条      |
    |   4 |         6 |         1 | 第一条      |
    |   4 |         6 |         2 | 第二条      |
    |   4 |         6 |         3 | 第三条      |
    +-----+-----------+-----------+-------------+
    12 rows in set (0.00 sec)
    

    可见select * from a,b是笛卡儿积


    再来验证select * from a,b where a.id=b.id

    mysql> select * from userinfo,report where userinfo.report_id=report.report_id;
    +-----+-----------+-----------+-------------+
    | uid | report_id | report_id | description |
    +-----+-----------+-----------+-------------+
    |   1 |         1 |         1 | 第一条      |
    |   2 |         1 |         1 | 第一条      |
    |   3 |         2 |         2 | 第二条      |
    +-----+-----------+-----------+-------------+
    3 rows in set (0.00 sec)
    

    inner join
    mysql> select * from userinfo inner join report where userinfo.report_id=report.report_id;
    +-----+-----------+-----------+-------------+
    | uid | report_id | report_id | description |
    +-----+-----------+-----------+-------------+
    |   1 |         1 |         1 | 第一条      |
    |   2 |         1 |         1 | 第一条      |
    |   3 |         2 |         2 | 第二条      |
    +-----+-----------+-----------+-------------+
    3 rows in set (0.00 sec)
    
    mysql> select * from userinfo inner join report on userinfo.report_id=report.report_id;
    +-----+-----------+-----------+-------------+
    | uid | report_id | report_id | description |
    +-----+-----------+-----------+-------------+
    |   1 |         1 |         1 | 第一条      |
    |   2 |         1 |         1 | 第一条      |
    |   3 |         2 |         2 | 第二条      |
    +-----+-----------+-----------+-------------+
    3 rows in set (0.00 sec)
    

    可见是select * from a,b where a.id=b.id只是把笛卡尔积做了一层过滤,结果与inner join相同

    补充:inner join是先生成一个临时表,然后使用on条件筛选

    注:以上结论只在mysql 5.7验证过,其他数据库不一定成立

  • 相关阅读:
    iOS 宏(define)与常量(const)的正确使用
    Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
    如何在 GitHub 建立个人主页和项目演示页面
    Git 使用指南(cmd + gui)
    Google C++ 编码规范(中文版)
    SVN服务器搭建和使用
    演化理解 Android 异步加载图片(转)
    Android之断点续传下载(转)
    Android学习笔记——关于onConfigurationChanged(转)
    (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
  • 原文地址:https://www.cnblogs.com/zzliu/p/11370272.html
Copyright © 2011-2022 走看看