sql server里如何给子查询设置别名

fanzhouqi 2010-03-22 11:00:47
declare @sId uniqueidentifier, @free int ,@booked int, @bookingTimeUp datetime
set @sId = '8F0EC5C0-7839-4C24-8974-9738F2C0F1DA'
set @free = 1
set @booked = 2
set @bookingTimeUp = getdate()
select
t.TableName
from
[Table] t
inner join
[TableType] tt
on t.[TableType]=tt.ID
inner join
[Shop] s
on tt.[Shop]= s.ID
inner join
[BookingTable] bt
on t.ID=bt.[Table]
inner join
[Booking] b
on bt.[Booking]=b.ID
where s.ID=@sId and (
(t.State=@free) or
(t.State=@booked and @bookingTimeUp <
all(select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID) A)
)

想将 select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
这个子查询设个别名,因为下面还要用到很多次,但我用上面的写法是报错的,不知道正确的应该怎么写???

...全文
965 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_Nice 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
如果是2005的话 直接把select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID放入CTE


2000的话放入临时表
[/Quote]

多次引用2005 用CTE
2000用临时表。
fanzhouqi 2010-03-23
  • 打赏
  • 举报
回复
to 5楼:你的解决方案在只能让A在一个地方出现,我在后面再引用A就报错了。

存到临时变量中不知该怎么写?因为这里的子查询是要根据父查询的ID进行查询的。
--小F-- 2010-03-22
  • 打赏
  • 举报
回复
如果是2005的话 直接把select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID放入CTE


2000的话放入临时表
黄_瓜 2010-03-22
  • 打赏
  • 举报
回复
建议不要使用all
and @bookingTimeUp < (
select min(BookingTime) from
(
select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
) A
)
--这样应该可以在其他地方使用表a
--你试试吧,不行就把数据插入到一个临时表或者便量表中使用
黄_瓜 2010-03-22
  • 打赏
  • 举报
回复
建议不要使用all
and @bookingTimeUp < (
select min(BookingTime) from
(
select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
) A
)
--这样应该可以在其他地方使用表a
--你试试吧,不行就把数据插入到一个临时表或者便量表中使用
黄_瓜 2010-03-22
  • 打赏
  • 举报
回复
and @bookingTimeUp < all(
select BookingTime from
(
select BookingTime from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
) A
)
东那个升 2010-03-22
  • 打赏
  • 举报
回复
select min(BookingTime) from [Booking] b1 
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
东那个升 2010-03-22
  • 打赏
  • 举报
回复
declare @sId uniqueidentifier, @free int ,@booked int, @bookingTimeUp datetime
set @sId = '8F0EC5C0-7839-4C24-8974-9738F2C0F1DA'
set @free = 1
set @booked = 2
set @bookingTimeUp = getdate()
select
t.TableName
from
[Table] t
inner join
[TableType] tt
on t.[TableType]=tt.ID
inner join
[Shop] s
on tt.[Shop]= s.ID
inner join
[BookingTable] bt
on t.ID=bt.[Table]
inner join
[Booking] b
on bt.[Booking]=b.ID
where s.ID=@sId and (
(t.State=@free) or
(t.State=@booked and @bookingTimeUp <
(select min(BookingTime) from [Booking] b1
inner join [BookingTable] bt2 on b1.ID = bt2.[Booking]
where bt2.[Table] = t.ID
) )
)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧