时间:2021-07-01 10:21:17 帮助过:33人阅读
MySQL-派生式
派生式和子查询通常可互换使用。当SELECT
语句的FROM
子句中使用独立子查询时,我们将其称为派生表
子查询不同,派生表必须具有别名,以便稍后在查询中引用其名称。
实例:
orders表和orderdetails表中获得2013年销售收入最高的前5名产品: SELECT productCode, ROUND(SUM(quantityOrdered * priceEach)) sales FROM orderdetails INNER JOIN orders USING (orderNumber) WHERE YEAR(shippedDate) = 2013 GROUP BY productCode ORDER BY sales DESC LIMIT 5;
MySQL-公共表达式(CTE)
公用表表达式是一个命名的临时结果集,仅在单个SQL语句的执行范围内存在。
查询顾客信息: WITH customers_in_usa AS ( SELECT customerName, state FROM customers WHERE country = ‘USA‘ ) SELECT customerName FROM customers_in_usa WHERE state = ‘CA‘ ORDER BY customerName;
MySQL-子查询,派生表,通用表达式
标签:employees customer number tom cte order by using 销售 mys