• MySQL内连接和外连接


    • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
    • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
    • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

    MySQL内连接(inner join on)

          MySQL的内连接使用inner join on,它的效果跟使用where是一样的,如果联结的是两个表,那么需要左右的条件或者说字段是需要完全匹配的。

          来看个例子:有两张表customers客户表和orders订单表,外键是cust_id,我们需要知道哪些客户有订单     

         select customers.cust_id,orders.order_num from customers , orders where customers.cust_id = orders.cust_id;

         如果我们使用内连接的话就可以这样写:

         select customers.cust_id,orders.order_num from customers inner join orders on customers.cust_id = orders.cust_id;

         但是如果我除了这些有有客户的订单,我还想拿到所有的订单信息,那么怎么办呢?

      MySQL外连接(left,right)

         select customers.cust_id,orders.order_num from customers right outer join orders on customers.cust_id = orders.cust_id;

         外连接包含左右连接,

         左连接的结果是除了匹配条件的数据还包含左边表中的所有数据

         右连接的结果是除了匹配条件的数据还包含右边表中的所有数据

  • 相关阅读:
    【C#】C#8.0常用新特性
    【Docker】Asp.net core在docker容器中的端口问题
    【gRPC】ProtoBuf 语言快速学习指南
    【日常排雷】 .Net core 生产环境appsetting读取失败
    【gRPC】 在.Net core中使用gRPC
    raspbian buster阿里镜像
    管道通信
    关于utf8mb4的使用
    ef core数据迁移的一点小感悟
    windows 端口转发
  • 原文地址:https://www.cnblogs.com/niuli1987/p/9683115.html
Copyright © 2020-2023  润新知