我正在尝试使用多个SQL With
子句。
我使用倍数的原因是我With
将此SQL发送到AS400项目。本With TEMP
必须是强制性的,而不是Temp2
说必须是可选的。
我不知道该怎么做。此SQL仍然引发错误:
With Temp2 As ( With Temp As ( Select Name, Surname, Age From People Where Age > 18 ) Select A.*, B.* From Temp A Left Join City B on B.Name = A.Name and B.Surname = A.Surname Where B.City = "Venice" ) Select * From Temp2 C Left Join State D on D.City = C.City
我想了解我该怎么做。
是的,任何CTE都可以引用在其之前创建的CTE。第一个CTE必须以“ With”开头,并以逗号结尾,以允许创建另一个CTE。
with temp as ( select name, surname, age from people where age > 18 ), temp2 as ( select a.*, b.* from temp a left join city b on b.name = a.name and b.surname = a.surname where b.city = "Venice" ) select * from temp2 c left join state d on d.city = c.city ;
这在功能上等同于下面的查询,该查询不需要任何CTE。
select * from people as a join city b on b.name = a.name and b.surname = a.surname and b.city = "Venice" left join state c on c.city = b.city where a.age > 18 ;