• mysql-联结


    一、联结

      联结是利用SQL的select能执行的最重要的操作。

      1、关系表:假如有一个包含产品目录的数据库表,其中每个类别的物品占一行。对于每种物品要求存储的信息包括产品描述和价格,以及生产该产品的供应商信息。

        现在假如有由同一供应商生产的多种物品,那么在何处存储供应商信息(如供应商名、地址、联系方式等)。

        由于相同数据出现多次违背了范式设计原则,因此在这个例子中,可以建立两个表,一个存储供应商信息vendors,另一个存储产品信息products。产品信息表中除了存储供应商ID外不存储其他供应商信息。因此vendors表的主键又叫做products的外键。他将vendors与products关联。

      2、外键(foreign key):外键为某个表中的一列,它包含另一个表中的主键值,定义了两个表之间的关系。

      

    二、创建联结

      联结是一种机制,用来在一条select语句中关联表,因此称为联结。使用特殊的语法,可以联结多个表返回一组输出。

      select vend_name,prod_name,prod_price from vendors,products where vendos.vend_id=products.vend_id order by vend_name,prod_name;

      为了匹配两个列以vender.vend_id和products.vend_id指定,需要使用完全限定名,防止引起歧义。

      笛卡尔积:由没有联结条件的表关系返回的结果为笛卡尔积。检索出的行数目将是第一个表中的行数乘以第二个表中的行数。

      select vend_name,prod_name,prod_price from vendors,products order by vend_name,prod_name;

    三、内部联结

      等值联结:它基于两个表之间的相等测试。可以使用:

      inner join on:

      select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id = products.vend_id;

      以上语句以inner join指定,在使用这种语法时,联结条件用特定的on子句而不是where子句给出。传递给on实际条件与传递给where相同。

      inner join on性能比where好。

    四、联结多个表

      SQL对一条select语句中可以联结的数目没有限制。创建联结基本规则也相同。首先列出所有表,然后定义表之间的关系。

      select prod_name,vend_name,prod_price,quantity from orderitems,products,vendors where products.vend_id=vendor.vend_id and orderitems.prod_id = products.prod_id and order_num=20005;

      联结的表越多,性能下降的越厉害。

      我们在之前使用的子查询其实也可以使用联结语句。如下:

      select cust_name,cust_contact from customers,orders,orderitems where customers.cust_id=orders.cust_id and orderitems.order_num=orders.order_num and prod_id='TNT2';

    而子查询中可以这样写:select cust_name,cust_contact form customers where cust_id in (select cust_id from orders where order_num in(select order_num from orderitems where prod_id='TNT2'))

    五、使用表别名

      不仅仅可以对列起别名,也可以对表起别名。

      select cust_name,cust_contact from customers as c,orders as o,orderitems as oi where c.cust_id = o.cust=id and oi.order_num=o.cust_id and oi.order_num=o.order_num and prod_id ='TNT2';

    六、使用不同类型的联结:

      我们在前面使用了内部联结和等值联结,其实还有更复杂的联结

      1、自联结:假如你发现某物品(ID为DTNTR)存在问题,因此想知道该物品的供应商生产的其他物品是否也存在这些问题,因此,你需要先查到生产ID为DTNTR的物品供应商,然后找出这个供应商生产的其他物品。自连接的速度非常好:

      select prod_id,prod_name from products where vend_id=(select vend_id form products where prod_id ='DTNTR');

      也可以使用别名:  

      select p1.prod_id,p1.prod_name from products as  p1, products as  p2 where p1.vend_id=p2.vend_id and p2.prod_id='DTNTR';

      

      2、外部连接

      许多连接将一个表中的行与另一个表中的行向关联,但有时候会需要包含没有关联行的那些行。

      外链接不包含在相关表中没有关联行的行,这种类型的联结称为外部联结,例如:

      1、对每个客户下了多少订单进行计数,包括那些至今尚未下订单的客户。

      2、列出所有产品以及订购数目,包括没有人订购的产品

      3、计算平均销售规模,包括那些至今尚未下订单的客户。

      select customers.cust_id,orders.order_num from customers left outer join orders on(customers.cust_id=orders.cust_id);

      以上语句我们使用了左外连接,目的是也关联左表并没有被查询关联数据。当然我们也可以使用右连接。

      select customers.cust_id,orders.order_num form customers right outer join orders on (orders.cust_id=customers.cust_id);

      七、使用带聚集函数的联结

      聚集函数也可以与联结一起使用。

      如果要检索所有客户及每个客户所下的订单数:

      select customers.cust_name,customers.cust_id,count(orders.order_num) as num_ord from customers inner join orders on customers.cust_id = orders.cust_id group by customers.cust_id;

      聚集函数也可以分方便的与其他联结一起使用。

      select customers.cust_name,customers.cust_id,count(orders.order_num) as num_ord from customers left outer orders on customers.cust_id = orders.cust_id group by customers.cust_id ;

      外部联结,把没下订单的客户也查出来了。

  • 相关阅读:
    .net core json 操作
    TypeScript 解构和展开
    asp.net core 学习路线
    NVM的安装和NPM下载速度慢的问题
    OzCode 最牛Visual Studio 调试工具
    破解EntityFramework Core
    动态WebAPI实现原理
    sqlyog 下载
    小程序生成二维码,海报
    .net core 拦截器的使用
  • 原文地址:https://www.cnblogs.com/television/p/8358496.html
Copyright © 2020-2023  润新知