美文网首页自动化测试之路
SQL数据库---INNER/LEFT/RIGHT JOIN的区

SQL数据库---INNER/LEFT/RIGHT JOIN的区

作者: orange_ade | 来源:发表于2020-10-19 16:28 被阅读0次
    演示数据库
    • 表:Websites
      +----+--------------+---------------------------+-------+---------+
      | id | name | url | num | country |
      +----+--------------+---------------------------+-------+---------+
      | 1 | Google | https://www.google.cm/ | 1 | USA |
      | 2 | orange | https://www.orange.com/ | 13 | CN |
      | 3 | vivid | http://www.vivid.com/ | 4689 | CN |
      | 4 | 微信 | http://weixin.com/ | 20 | CN |
      | 5 | Facebook | https://www.facebook.com/ | 3 | USA |
      | 7 | douyin | http://douyin.com/ | 0 | IND |
      +----+---------------+---------------------------+-------+---------+
    • 表:work_log
      +-----+---------+-------+------------+
      | aid | site_id | count | date |
      +-----+---------+-------+------------+
      | 1 | 1 | 45 | 2020-05-10 |
      | 2 | 3 | 100 | 2020-05-13 |
      | 3 | 1 | 230 | 2020-05-14 |
      | 4 | 2 | 10 | 2020-05-14 |
      | 6 | 4 | 13 | 2020-05-15 |
      | 7 | 3 | 220 | 2020-05-15 |
      | 8 | 5 | 545 | 2020-05-16 |
      | 9 | 10 | 111 | 2020-05-18 |
      +-----+---------+-------+------------+
    一、INNER JOIN
    • INNER JOIN 关键字在表中存在至少一个匹配时返回行。
      例如:
      SELECT Websites.name, work_log.count, work_log.date
      FROM Websites
      INNER JOIN work_log
      ON Websites.id=work_log.site_id
      ORDER BY work_log.count;

    +-----+---------+-------+------------+
    | name | count | date |
    +-----+---------+-------+------------+
    | orange | 10 | 2020-05-13 |
    | 微信 | 13 | 2020-05-15 |
    | Google | 45 | 2020-05-10 |
    | vivid | 100 | 2020-05-13 |
    | vivid | 220 | 2020-05-15 |
    | Google | 230 | 2020-05-14 |
    | Facebook | 545 | 2020-05-16 |
    +-----+---------+-------+------------+

    二、LEFT JOIN
    • LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。
      例如:
      SELECT Websites.name, work_log.count, work_log.date
      FROM Websites
      LEFT JOIN work_log
      ON Websites.id=work_log.site_id
      ORDER BY work_log.count DESC;

    +-----+---------+-------+------------+
    | name | count | date |
    +-----+---------+-------+------------+
    | Facebook | 545 | 2020-05-16 |
    | Google | 230 | 2020-05-14 |
    | vivid | 220 | 2020-05-15 |
    | vivid | 100 | 2020-05-13 |
    | Google | 45 | 2020-05-10 |
    | 微信 | 13 | 2020-05-15 |
    | orange | 10 | 2020-05-13 |

    | douyin | NULL | NULL | (右表没有匹配记录也会显示)

    +-----+---------+-------+------------+

    三、RIGHT JOIN
    • RIGHT JOIN 关键字从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
      例如:
      SELECT websites.name, work_log.count, work_log.date
      FROM websites
      RIGHT JOIN work_log
      ON work_log.site_id=websites.id
      ORDER BY work_log.count DESC;

    +-----+---------+-------+------------+
    | name | count | date |
    +-----+---------+-------+------------+
    | Facebook | 545 | 2020-05-16 |
    | Google | 230 | 2020-05-14 |
    | vivid | 220 | 2020-05-15 |

    | NULL | 111 | 2020-05-18 | (左表没有匹配记录也会显示)

    | vivid | 100 | 2020-05-13 |
    | Google | 45 | 2020-05-10 |
    | 微信 | 13 | 2020-05-15 |
    | orange | 10 | 2020-05-13 |
    +-----+---------+-------+------------+

    相关文章

      网友评论

        本文标题:SQL数据库---INNER/LEFT/RIGHT JOIN的区

        本文链接:https://www.haomeiwen.com/subject/ilakmktx.html