sql MS SQL内部连接与子查询与CTE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL内部连接与子查询与CTE相关的知识,希望对你有一定的参考价值。

-- Database : AdventureWorks2014
-- Get All Product Names with ListPrice similar
-- to that of the Product 'LL Mountain Seat Assembly'


-- USING A NORMAL INNER JOIN
SELECT P1. Name
FROM   Production.Product AS P1
       JOIN Production.Product AS P2
         ON P1.ListPrice = P2.ListPrice
WHERE  P2. Name = 'LL Mountain Seat Assembly';


-- USING SIMPLE SUB QUERY
--  WHERE = (Scalar Subquery)
SELECT P.Name
FROM   Production.Product P
WHERE  P.ListPrice = (SELECT ListPrice
            FROM   Production.Product
            WHERE  Name = 'LL Mountain Seat Assembly');


--- USING CTE (COMMON TABLE EXPRESSION)
WITH LP (ListPrice)
     AS (SELECT ListPrice
         FROM   Production.Product
         WHERE  Name = 'LL Mountain Seat Assembly')
SELECT P.Name
FROM   Production.Product P
WHERE  P.ListPrice = (SELECT ListPrice
            FROM   LP); 

以上是关于sql MS SQL内部连接与子查询与CTE的主要内容,如果未能解决你的问题,请参考以下文章

SQL 查询时间复杂度 - 连接与子查询

通过 MS Access 例程更新 SQL Server(内部连接错误)

T-SQL:CTE用法

SQL查询,外连接,cte?需要使用“左”值修复运行总计

sql MS SQL CTE

选择中的左连接与子查询的奇怪问题