我的查询语句是:
select top 5 * from SStand where SStandID not in(select top 3 SStandID from SStand
inner join containstable(SStand,([SStandFRContent],[SStandGRContent]),'安全',50) as k
on SStandID=k.[key]
where SStandTypeID=CONVERT(nvarchar,7)
order by SStandID asc)
此语句没有语法错误,解释一下
select top 3 SStandID from SStand
inner join containstable(SStand,([SStandFRContent],[SStandGRContent]),'安全',50) as k
on SStandID=k.[key]
where SStandTypeID=CONVERT(nvarchar,7)
order by SStandID asc
这个查询语句的结果假设为
id name .....
1 a
3 c
4 d
假如总的查询是:
id name .....
1 a
3 c
4 d
5 e
6 f
7 g
8 h
9 j
想要得到的结果是:
id name .....
5 e
6 f
7 g
8 h
9 j
但是查询总的语句时出现的结果却是
id name...
2 n
5 e
6 f
7 g
8 h
原因分析: 我这个语句是对的。但就是筛选缺少条件,缺少一个
select * SStandID from SStand
inner join containstable(SStand,([SStandFRContent],[SStandGRContent]),'安全',50) as k
on SStandID=k.[key]
where SStandTypeID=CONVERT(nvarchar,7)
order by SStandID asc
的条件。 not in里面是有一个这样的查询句子,但那个句子只筛选出来了你不要的数据,并没有筛选出来你所要的数据
差上面的那个查询语句,可是我不知道这个语句要放到哪里。
求解。。。。。
给个对照:
select top 5 * from SStand where SStandID not in(select top 3 SStandID from SStand where contains(SStandFRContent,'安全')order by SStandID) and Contains(SStandFRContent,'安全')
我缺少的就是这个红色类似的查询语句
12 个解决方案
#1
charindex('安全',SStandFRContent)>0
#2
嵌套后加个where条件?用like不行吗?
#3
我的东西都是用我上面的方法做的。 改起来很麻烦。 charindex(....) 这个方法比较古老 我用过。 但不适用我这个
谢谢你。
#4
我的数据很多呢。 like能不用尽量不用。
这个问题简单一点的理解就是
我希望在查询的一个结果集中 再对结果集进行查询。
#5
你的逻辑有问题
top 3有安全条件
top 5 没有安全条件
top 3有安全条件
top 5 没有安全条件
#6
;with cte as (
select top 8 SStand.*,row_number() over(order by SStandID asc) as num from SStand
inner join containstable(SStand,([SStandFRContent],[SStandGRContent]),'安全',50) as k
on SStandID=k.[key]
where SStandTypeID=CONVERT(nvarchar,7)
order by SStandID asc
)
select * from cte
where num >3
#7
cte是为这个逻辑设置的
#8
没错,没错。 我就是在想。 子查询或者上面的 可不可以带上安全条件。。
#9
可以给点注释吗? 这个有点不懂。。。
#10
可以放入临时表 可以用 CTE
#11
放入临时表。假如有两个人同时查询。 有假如两个人都用到了临时表,里面的数据会不会被后面一个人的查询结果覆盖?
#12
应该不会 在一个事务中先不要DROP掉
实在害怕的话可以考虑用全局临时表
#1
charindex('安全',SStandFRContent)>0
#2
嵌套后加个where条件?用like不行吗?
#3
我的东西都是用我上面的方法做的。 改起来很麻烦。 charindex(....) 这个方法比较古老 我用过。 但不适用我这个
谢谢你。
#4
我的数据很多呢。 like能不用尽量不用。
这个问题简单一点的理解就是
我希望在查询的一个结果集中 再对结果集进行查询。
#5
你的逻辑有问题
top 3有安全条件
top 5 没有安全条件
top 3有安全条件
top 5 没有安全条件
#6
;with cte as (
select top 8 SStand.*,row_number() over(order by SStandID asc) as num from SStand
inner join containstable(SStand,([SStandFRContent],[SStandGRContent]),'安全',50) as k
on SStandID=k.[key]
where SStandTypeID=CONVERT(nvarchar,7)
order by SStandID asc
)
select * from cte
where num >3
#7
cte是为这个逻辑设置的
#8
没错,没错。 我就是在想。 子查询或者上面的 可不可以带上安全条件。。
#9
可以给点注释吗? 这个有点不懂。。。
#10
可以放入临时表 可以用 CTE
#11
放入临时表。假如有两个人同时查询。 有假如两个人都用到了临时表,里面的数据会不会被后面一个人的查询结果覆盖?
#12
应该不会 在一个事务中先不要DROP掉
实在害怕的话可以考虑用全局临时表