zoukankan      html  css  js  c++  java
  • 程序媛计划——mysql连接表

    #inner join等值连接/内连接

    mysql> select * from info;
    +------+-------------+----------+
    | name | phone       | address  |
    +------+-------------+----------+
    | zhao | 13199999999 | Beijing  |
    | qian | 1328888888  | Harbin   |
    | sun  | 13366666666 | Shanghai |
    +------+-------------+----------+
    3 rows in set (0.00 sec)
    mysql> select * from score;
    +----+------+-------+------------+
    | id | name | score | evaluation |
    +----+------+-------+------------+
    |  1 | zhao | 98.00 | 10         |
    |  2 | qian | 90.30 | 10         |
    |  3 | sun  | 70.00 | 10         |
    |  4 | li   | 80.50 | 10         |
    +----+------+-------+------------+
    4 rows in set (0.00 sec)

    #inner join——按照相同的name值(筛选行)连接两个表中的某些字段(筛选列)构成表

    mysql> select score.id,score.name,info.phone from score inner join info on score.name=info.name;
    +----+------+-------------+
    | id | name | phone       |
    +----+------+-------------+
    |  1 | zhao | 13199999999 |
    |  2 | qian | 1328888888  |
    |  3 | sun  | 13366666666 |
    +----+------+-------------+
    3 rows in set (0.00 sec)

    #left join——读取左边表score的字段的所有值,按照相同的name值连接两个表中的字段构成表(右边的表没有对应记录的填null)

    mysql> select score.id,score.name,info.phone from score left join info on score.name=info.name;
    +----+------+-------------+
    | id | name | phone       |
    +----+------+-------------+
    |  1 | zhao | 13199999999 |
    |  2 | qian | 1328888888  |
    |  3 | sun  | 13366666666 |
    |  4 | li   | NULL        |
    +----+------+-------------+
    4 rows in set (0.00 sec)

    #类似的有right join

    mysql> select score.id,score.name,info.phone from score right join info on score.name=info.name;
    +------+------+-------------+
    | id   | name | phone       |
    +------+------+-------------+
    |    1 | zhao | 13199999999 |
    |    2 | qian | 1328888888  |
    |    3 | sun  | 13366666666 |
    +------+------+-------------+
    3 rows in set (0.00 sec)
  • 相关阅读:
    0045算法笔记——【随机化算法】舍伍德随机化思想搜索有序表
    精进~如何成为很厉害的人
    哪些小习惯一旦养成终生受用?
    2016第24周四
    2016第24周三
    2016第24周二
    2016第24周一
    2016第23周日
    前端资源汇总
    2016第23周五
  • 原文地址:https://www.cnblogs.com/IcarusYu/p/7497532.html
Copyright © 2011-2022 走看看