zoukankan      html  css  js  c++  java
  • [转]MySQL Left Join学习例子

    mysql> CREATE TABLE `product` (
      `id` int(10) unsigned NOT NULL auto_increment,
      `amount` int(10) unsigned default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
     
    mysql> CREATE TABLE `product_details` (
      `id` int(10) unsigned NOT NULL,
      `weight` int(10) unsigned default NULL,
      `exist` int(10) unsigned default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1
     
    mysql> INSERT INTO product (id,amount)
           VALUES (1,100),(2,200),(3,300),(4,400);
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
     
    mysql> INSERT INTO product_details (id,weight,exist)
           VALUES (2,22,0),(4,44,1),(5,55,0),(6,66,1);
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
     
    mysql> SELECT * FROM product;
    +----+--------+
    | id | amount |
    +----+--------+
    |  1 |    100 |
    |  2 |    200 |
    |  3 |    300 |
    |  4 |    400 |
    +----+--------+
    4 rows in set (0.00 sec)
     
    mysql> SELECT * FROM product_details;
    +----+--------+-------+
    | id | weight | exist |
    +----+--------+-------+
    |  2 |     22 |     0 |
    |  4 |     44 |     1 |
    |  5 |     55 |     0 |
    |  6 |     66 |     1 |
    +----+--------+-------+
    4 rows in set (0.00 sec)
     
    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id);
    +----+--------+------+--------+-------+
    | id | amount | id   | weight | exist |
    +----+--------+------+--------+-------+
    |  1 |    100 | NULL |   NULL |  NULL |
    |  2 |    200 |    2 |     22 |     0 |
    |  3 |    300 | NULL |   NULL |  NULL |
    |  4 |    400 |    4 |     44 |     1 |
    +----+--------+------+--------+-------+
    4 rows in set (0.00 sec)

    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id)
           AND product_details.id=2;
    +----+--------+------+--------+-------+
    | id | amount | id   | weight | exist |
    +----+--------+------+--------+-------+
    |  1 |    100 | NULL |   NULL |  NULL |
    |  2 |    200 |    2 |     22 |     0 |
    |  3 |    300 | NULL |   NULL |  NULL |
    |  4 |    400 | NULL |   NULL |  NULL |
    +----+--------+------+--------+-------+
    4 rows in set (0.00 sec)

    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id)
           WHERE product_details.id=2;
    +----+--------+----+--------+-------+
    | id | amount | id | weight | exist |
    +----+--------+----+--------+-------+
    |  2 |    200 |  2 |     22 |     0 |
    +----+--------+----+--------+-------+
    1 row in set (0.01 sec)

    mysql>
    mysql> SELECT * FROM product LEFT JOIN product_details
           ON product.id = product_details.id
           AND product.amount=100;
    +----+--------+------+--------+-------+
    | id | amount | id   | weight | exist |
    +----+--------+------+--------+-------+
    |  1 |    100 | NULL |   NULL |  NULL |
    |  2 |    200 | NULL |   NULL |  NULL |
    |  3 |    300 | NULL |   NULL |  NULL |
    |  4 |    400 | NULL |   NULL |  NULL |
    +----+--------+------+--------+-------+
    4 rows in set (0.00 sec)

    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id)
           AND product.amount=200;
    +----+--------+------+--------+-------+
    | id | amount | id   | weight | exist |
    +----+--------+------+--------+-------+
    |  1 |    100 | NULL |   NULL |  NULL |
    |  2 |    200 |    2 |     22 |     0 |
    |  3 |    300 | NULL |   NULL |  NULL |
    |  4 |    400 | NULL |   NULL |  NULL |
    +----+--------+------+--------+-------+
    4 rows in set (0.01 sec)

    mysql> SELECT a.* FROM product a LEFT JOIN product_details b
           ON a.id=b.id AND b.weight!=44 AND b.exist=0
           WHERE b.id IS NULL;
    +----+--------+
    | id | amount |
    +----+--------+
    |  1 |    100 |
    |  3 |    300 |
    |  4 |    400 |
    +----+--------+
    3 rows in set (0.00 sec)

    mysql> SELECT a.* FROM product a LEFT JOIN product_details b
           ON a.id=b.id AND b.weight!=44 AND b.exist=1
           WHERE b.id IS NULL;
    +----+--------+
    | id | amount |
    +----+--------+
    |  1 |    100 |
    |  2 |    200 |
    |  3 |    300 |
    |  4 |    400 |
    +----+--------+
    4 rows in set (0.00 sec)

    mysql> SELECT a.* FROM product a LEFT JOIN product_details b
           ON a.id=b.id
           WHERE b.id is null OR b.weight=44 OR b.exist=1;
    +----+--------+
    | id | amount |
    +----+--------+
    |  1 |    100 |
    |  3 |    300 |
    |  4 |    400 |
    +----+--------+
    3 rows in set (0.00 sec)
     
     
     
  • 相关阅读:
    shell-脚本_系统监测
    shell-脚本_防火墙规则的简单应用
    shell-命令_find
    shell-命令_cut
    shell-条件测试语句_test
    Error:java: 不再支持源选项 5。请使用 6 或更高版本。
    android项目删除recycleview相对应的数据库数据后闪退
    大作业--社团管理系统总结
    北京地铁出行路线代码分析
    北京地铁出行路线规划设计
  • 原文地址:https://www.cnblogs.com/sunson/p/2288967.html
Copyright © 2011-2022 走看看