mysql派生表(Derived Table)简单用法实例解析
本文实例讲述了mysql派生表(derived table)简单用法。分享给大家供大家参考,具体如下:
关于这个派生表啊,我们首先得知道,派生表是从select语句返回的虚拟表。派生表类似于临时表,但是在select语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤。所以当select语句的from子句中使用独立子查询时,我们将其称为派生表。废话不多说,我们来具体的解释:
select column_list from * (select * column_list * from * table_1) derived_table_name; where derived_table_name.column > 1...
其中标记星号的地方就使用了派生表。为了详细点,咱们来看个具体的例子。咱们接下来要从数据库中的orders表和orderdetails表中获得2018年销售收入最高的前5名产品。先来看下表的字段:
咱们先来看下面这条sql:
select productcode, round(sum(quantityordered * priceeach)) sales from orderdetails inner join orders using (ordernumber) where year(shippeddate) = 2018 group by productcode order by sales desc limit 5;
这条sql是以两张表*有的ordernumber字段为联合查询的节点,完事之后,以时间为条件,再以那个什么productcode字段为分组依据,完事获取分组字段和计算之后的别称字段,再以sales字段为排序依据,最后提取前五条结果。大概就是这么回事,完事结果集我们可以看做是一张临时表或者别的什么。大家来看个结果集:
+-------------+--------+ | productcode | sales | +-------------+--------+ | s18_3232 | 103480 | | s10_1949 | 67985 | | s12_1108 | 59852 | | s12_3891 | 57403 | | s12_1099 | 56462 | +-------------+--------+ 5 rows in set
完事呢,既然是学习派生表,我们当然可以使用此查询的结果作为派生表,并将其与products表相关联。其中,products表的结构如下所示:
mysql> desc products; +--------------------+---------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +--------------------+---------------+------+-----+---------+-------+ | productcode | varchar(15) | no | pri | | | | productname | varchar(70) | no | | null | | | productline | varchar(50) | no | mul | null | | | productscale | varchar(10) | no | | null | | | productvendor | varchar(50) | no | | null | | | productdescription | text | no | | null | | | quantityinstock | smallint(6) | no | | null | | | buyprice | decimal(10,2) | no | | null | | | msrp | decimal(10,2) | no | | null | | +--------------------+---------------+------+-----+---------+-------+ 20 rows in set
表结构既然了解完事了,我们就来看下面的sql:
select productname, sales from # (select # productcode, # round(sum(quantityordered * priceeach)) sales # from # orderdetails # inner join orders using (ordernumber) # where # year(shippeddate) = 2018 # group by productcode # order by sales desc # limit 5) top5_products_2018 inner join products using (productcode);
上面#号部分是咱们之前的那条sql,方便大家理解,我使用#标记了出来,大家写的时候可不能用啊。完事我们来看下这条sql是神马意思呢?它是把我们用#标记的部分当做一个表,来做一个简单的联合查询而已。然而这个表,我们就叫它派生表,它会在使用过后即时清除的,所以我们在简化复杂查询的时候可以考虑使用。废话不多说,我们来看下结果集:
+-----------------------------+--------+ | productname | sales | +-----------------------------+--------+ | 1992 ferrari 360 spider red | 103480 | | 1952 alpine renault 1300 | 67985 | | 2001 ferrari enzo | 59852 | | 1969 ford falcon | 57403 | | 1968 ford mustang | 56462 | +-----------------------------+--------+ 5 rows in set
然后呢,咱们再来简单总结下:
- 首先,执行子查询来创建一个结果集或派生表。
- 然后,在productcode列上使用products表连接top5_products_2018派生表的外部查询。
完事呢,简单的派生表的理解和使用就到这里了。咱们再来一个稍稍复杂的来尝尝味道哈,首先假设必须将2018年的客户分为3组:铂金,白金和白银。 此外,需要了解每个组中的客户数量,具体情况如下:
- 订单总额大于100000的为铂金客户;
- 订单总额为10000至100000的为黄金客户
- 订单总额为小于10000的为银牌客户
要构建此查询,首先,我们需要使用case表达式和group by子句将每个客户放入相应的分组中,如下所示:
select customernumber, round(sum(quantityordered * priceeach)) sales, (case when sum(quantityordered * priceeach) < 10000 then 'silver' when sum(quantityordered * priceeach) between 10000 and 100000 then 'gold' when sum(quantityordered * priceeach) > 100000 then 'platinum' end) customergroup from orderdetails inner join orders using (ordernumber) where year(shippeddate) = 2018 group by customernumber order by sales desc;
咱们来看下结果集的实例:
+----------------+--------+---------------+ | customernumber | sales | customergroup | +----------------+--------+---------------+ | 141 | 189840 | platinum | | 124 | 167783 | platinum | | 148 | 150123 | platinum | | 151 | 117635 | platinum | | 320 | 93565 | gold | | 278 | 89876 | gold | | 161 | 89419 | gold | | ************此处省略了many数据 *********| | 219 | 4466 | silver | | 323 | 2880 | silver | | 381 | 2756 | silver | +----------------+--------+---------------+
完事嘞,咱们就可以使用上面的查询所得的表作为派生表来进行关联查询并且进行分组,获取想要的数据了,咱们来看下面的sql感受一下:
select customergroup, count(cg.customergroup) as groupcount from (select customernumber, round(sum(quantityordered * priceeach)) sales, (case when sum(quantityordered * priceeach) < 10000 then 'silver' when sum(quantityordered * priceeach) between 10000 and 100000 then 'gold' when sum(quantityordered * priceeach) > 100000 then 'platinum' end) customergroup from orderdetails inner join orders using (ordernumber) where year(shippeddate) = 2018 group by customernumber) cg group by cg.customergroup;
具体是啥意思,相信聪明如大家肯定比我有更好的理解了,咱就不赘述了。完事来看下结果集:
+---------------+------------+ | customergroup | groupcount | +---------------+------------+ | gold | 61 | | platinum | 4 | | silver | 8 | +---------------+------------+ 3 rows in set
得嘞,咱就到这里了。