gpt4 book ai didi

sql-server - SQL查询的执行顺序

转载 作者:行者123 更新时间:2023-12-01 20:19:09 25 4
gpt4 key购买 nike

我对此查询的执行顺序感到困惑,请解释一下。我对何时应用联接、调用函数、随案例添加新列以及何时添加序列号感到困惑。请解释一下这一切的执行顺序。

select Row_number() OVER(ORDER BY (SELECT 1))  AS 'Serial Number', 
EP.FirstName,Ep.LastName,[dbo].[GetBookingRoleName](ES.UserId,EP.BookingRole) as RoleName,
(select top 1 convert(varchar(10),eventDate,103)from [3rdi_EventDates] where EventId=13) as EventDate,
(CASE [dbo].[GetBookingRoleName](ES.UserId,EP.BookingRole)
WHEN '90 Day Client' THEN 'DC'
WHEN 'Association Client' THEN 'DC'
WHEN 'Autism Whisperer' THEN 'DC'
WHEN 'CampII' THEN 'AD'
WHEN 'Captain' THEN 'AD'
WHEN 'Chiropractic Assistant' THEN 'AD'
WHEN 'Coaches' THEN 'AD'
END) as Category from [3rdi_EventParticipants] as EP
inner join [3rdi_EventSignup] as ES on EP.SignUpId = ES.SignUpId
where EP.EventId = 13
and userid in (
select distinct userid from userroles
--where roleid not in(6,7,61,64) and roleid not in(1,2))
where roleid not in(19, 20, 21, 22) and roleid not in(1,2))

这是从上面的查询中调用的函数。

CREATE function [dbo].[GetBookingRoleName]  
(
@UserId as integer,
@BookingId as integer
)
RETURNS varchar(20)
as
begin
declare @RoleName varchar(20)

if @BookingId = -1
Select Top 1 @RoleName=R.RoleName From UserRoles UR inner join Roles R on UR.RoleId=R.RoleId Where UR.UserId=@UserId and R.RoleId not in(1,2)
else
Select @RoleName= RoleName From Roles where RoleId = @BookingId

return @RoleName
end

最佳答案

查询通常按以下顺序处理(SQL Server)。我不知道其他 RDBMS 是否这样做。

FROM [MyTable]
ON [MyCondition]
JOIN [MyJoinedTable]
WHERE [...]
GROUP BY [...]
HAVING [...]
SELECT [...]
ORDER BY [...]

关于sql-server - SQL查询的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4596467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com