帮我看看select top 5 * from titles where 为什么错误

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:47:10
select top 5 * from titles where pub_id not in (select top 5*(3-1) pub_id from titles where pub_id=1389)
这条语句为什么错了
我知道是这个地方5*(3-1)。
但是不知道如何修复

declare @num integer
declare @sql varchar(500)
set @num = 5*(3-1)
set @sql = 'select top 5 * from titles where pub_id not in (select top ' + cast(@num as varchar) + ' pub_id from titles where pub_id=1389)'
exec(@sql)
像这么写,前面的top 5也可以考虑用变量
如果是在程序中直接这样就行"..." + (5*(3-1)).ToString() + ".."

明显写法有问题,把多余的删掉

select top 5 * from titles where pub_id <> 1389

select top 5 * from titles where pub_id not in (select top (5*(3-1)) pub_id from titles where pub_id=1389)

这样写,要用括号括起来!