一个sql编译不过去

sunke5843323 2008-04-17 02:49:40

insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234,BoardID,0 from Temp_Assign where CommType='04' order by EleUsedCount
union
select top 2 234,BoardID,0 from Temp_Assign where CommType='05' order by EleUsedCount
...全文
90 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunke5843323 2008-04-17
  • 打赏
  • 举报
回复
我想分别取 CommType='04' 的EleUsedCount 最小的一条记录
CommType='05' 的EleUsedCount 最小的两条记录



insert into ProjectDetail(MainID,BoardID,Besure)
select 234,BoardID,0 from
(
select top 1 BoardID,EleUsedCount from Temp_Assign where CommType='04'
union
select top 2 BoardID,EleUsedCount from Temp_Assign where CommType='05'
)
a
order by CommType,EleUsedCount
这样写先取后排,结果是不是不正确啊
virusswb 2008-04-17
  • 打赏
  • 举报
回复
insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 [234],[BoardID],[0] from Temp_Assign where CommType='04' order by EleUsedCount
union
select top 2 [234],[BoardID],[0] from Temp_Assign where CommType='05' order by EleUsedCount

你的列名怎么可以使用数字呢,数字开头是不对的
boblaw 2008-04-17
  • 打赏
  • 举报
回复
上面寫錯了,改為如下:

insert into ProjectDetail(MainID,BoardID,Besure)
select 234,BoardID,0 from (select top 1 BoardID from Temp_Assign where CommType='04' order by EleUsedCount) a
union
select 234,BoardID,0 from (select top 2 BoardID from Temp_Assign where CommType='05' order by EleUsedCount)b

sunke5843323 2008-04-17
  • 打赏
  • 举报
回复
insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234,BoardID,0 from Temp_Assign where CommType='04'
union select top 2 234,BoardID,0 from Temp_Assign where CommType='05' order by EleUsedCount
这样写的话第一个select会不会排序呢
Tord_zhang 2008-04-17
  • 打赏
  • 举报
回复
values
boblaw 2008-04-17
  • 打赏
  • 举报
回复
修改如下:

insert into ProjectDetail(MainID,BoardID,Besure)
select 234,BoardID,0 from
(
select top 1 BoardID,EleUsedCount from Temp_Assign where CommType='04'
union
select top 2 BoardID,EleUsedCount from Temp_Assign where CommType='05'
)
a
order by CommType,EleUsedCount
hackztx 2008-04-17
  • 打赏
  • 举报
回复

insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234,BoardID,0 from Temp_Assign where CommType='04'
union
select top 2 234,BoardID,0 from Temp_Assign where CommType='05' order by EleUsedCount
叶子 2008-04-17
  • 打赏
  • 举报
回复
要注意对应类型的匹配
叶子 2008-04-17
  • 打赏
  • 举报
回复
分开插入就行了

insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 字段1,字段2,字段3 from Temp_Assign where CommType='04' order by EleUsedCount ;
insert into ProjectDetail(MainID,BoardID,Besure)
select top 2 字段1,字段2,字段3 from Temp_Assign where CommType='05' order by EleUsedCount ;
hackztx 2008-04-17
  • 打赏
  • 举报
回复
[code=SQL]
insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234,BoardID,0 from Temp_Assign where CommType='04'
union
select top 2 234,BoardID,0 from Temp_Assign where CommType='05' order by EleUsedCount
[\code]
zccmy22 2008-04-17
  • 打赏
  • 举报
回复
insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234 as MainID,BoardID,0 as Besure from Temp_Assign where CommType='04' order by EleUsedCount
union
select top 2 234 as MainID,BoardID,0 as Besure from Temp_Assign where CommType='05' order by EleUsedCount
贫僧又回来了 2008-04-17
  • 打赏
  • 举报
回复
什么错误啊?
zccmy22 2008-04-17
  • 打赏
  • 举报
回复
insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 [234],BoardID,[0] from Temp_Assign where CommType='04' order by EleUsedCount
union
select top 2 [234],BoardID,[0] from Temp_Assign where CommType='05' order by EleUsedCount

数字字段要加[]

如果不是字段。

insert into ProjectDetail(MainID,BoardID,Besure)
select top 1 234 as a,BoardID,0 as b from Temp_Assign where CommType='04' order by EleUsedCount
union
select top 2 234 as a,BoardID,0 as b from Temp_Assign where CommType='05' order by EleUsedCount
叶子 2008-04-17
  • 打赏
  • 举报
回复
234,BoardID,0这是字段名吗?

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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