写错,上面的例如, 应该ttitle=5
解决方案 »
- 我有一堆列表,是拆开一条一条执行效率高,还是凑成一句SQL执行效率高?
- 请指教:我想脱离windows2003中文版,安装新的windows2008中文版和vs.net2008中文版,就是不知能带来哪些不便和冲突。我现在用的有VB6、水晶报表9.2,office2003中文版、SQL SERVER 2000+sp3a、Inst
- 在线提问,解决结贴:用什么语句能得出我想要的结果?
- 大家帮我看看这个错误到底是怎么回事?
- SQL存储过程速度问题,查询分析器只要1秒,存储过程30秒多
- SQL Server 2005中如何自定义函数,实现类似Min或MAX函数功能。(支持者有分)
- 我在登陆里新建一个登陆但是在查询分析器连接时出错,出错提示如下:
- 如何取得数据库表中新增的数据?
- 如何在插入记录以前锁住表,不允许别的insert,update,del动作?
- java调用存储过程的问题!!又搞到晚上11:00那位帮帮忙看看
- 如何得到2个日期(smalldateTime)间隔天数??神仙来帮我啊
- 特殊的多表查询
表 news
nid ntitle nclude
1 来于于 1,3
2 来吧,朋友 1
3 高兴 3,2
4 同意 2,5
5 互动式 1,2,3,4,5表title
tid ttitle tname
1 1 修文来源
2 1 历史文化
3 2 文化咨询
4 2 旅游咨询
5 3 消费指南
A.*
FROM news A,
title B
WHERE B.tid = 1 -- 查询的条件
AND CHARINDEX(',' + RTRIM(B.tid) + ',', ',' + A.nclude + ',') > 0
结果
返回
tid ttitle tname
4 同意 2,5
5 互动式 1,2,3,4,5
将 varchar 值 '修文概况' 转换为数据类型为 int 的列时发生语法错误
如 ttitle 里有几条都是 1
如就ttitle=2
就有两条
tid ttitle tname
3 2 文化咨询
4 2 旅游咨询
再用些去查询news里
应该返回
就有
nid ntitle nclude
1 来于于 1,3
3 高兴 3,2
5 互动式 1,2,3,4,5
SELECT
A.*
FROM news A,
title B
WHERE B.ttitle = 1 -- 查询的条件
AND CHARINDEX(',' + RTRIM(B.tid) + ',', ',' + A.nclude + ',') > 0
Set Nocount On
declare @news table([nid] int,[ntitle] nvarchar(5),[nclude] nvarchar(9))
Insert @news
select 1,N'来于于',N'1,3' union all
select 2,N'来吧,朋友',N'1' union all
select 3,N'高兴',N'3,2' union all
select 4,N'同意',N'2,5' union all
select 5,N'互动式',N'1,2,3,4,5'declare @title table([tid] int,[ttitle] int,[tname] nvarchar(4))
Insert @title
select 1,1,N'修文来源' union all
select 2,1,N'历史文化' union all
select 3,2,N'文化咨询' union all
select 4,2,N'旅游咨询' union all
select 5,3,N'消费指南'Declare @ttitle int
Set @ttitle=2Select * From @news a
Where Exists(Select 1 From @title Where ttitle=@ttitle And Charindex(','+Rtrim(tid)+',',','+Rtrim(a.nclude)+',')>0)/*
nid ntitle nclude
----------- ------ ---------
1 来于于 1,3
3 高兴 3,2
5 互动式 1,2,3,4,5
*/
insert into news select 1,'来于于','1,3'
insert into news select 2,'来吧,朋友','1'
insert into news select 3,'高兴','3,2'
insert into news select 4,'同意','2,5'
insert into news select 5,'互动式','1,2,3,4,5'create table title(tid int,ttitle int, tname nvarchar(10))
insert into title select 1,1,'修文来源'
insert into title select 2,1,'历史文化'
insert into title select 3,2,'文化咨询'
insert into title select 4,2,'旅游咨询'
insert into title select 5,3,'消费指南'select ntitle from news where nclude like '%'+convert(varchar,1)+'%'
--如果要从title表间接查:
select ntitle from news where nclude like '%'+convert(varchar,(select top 1 tid from title where tname='修文来源'))+'%'
go
drop table news,title
/*
ntitle
----------
来于于
来吧,朋友
互动式
*/
所以,我改成这样
SELECT
A.*
FROM news A,
title B
WHERE B.ntitle= 2 -- 查询的条件
AND CHARINDEX(',' + RTRIM(B.nid) + ',', ',' + rtrim(A.nclude) + ',') > 0
再说,上面他写的nid是我表的主键
而ntitle不是主键
用上面的语句后,提示
将 varchar 值 '修文概况' 转换为数据类型为 int 的列时发生语法错误
Rtrim(a.nclude) 可以不用Rtrim()函数
------------------------------- 你不是查 ntitle 吗? 这个不是字符数据吗? 你用 2 干嘛?
select distinct news.* from
(select tid from title where ttitle=2) a cross join news
where nclude like '%'+cast(tid as varchar(2))+'%'结果:1 来于于 1,3
3 高兴 3,2
5 互动式 1,2,3,4,5
里,我是用ntitle来查询且里面有2呀
所有就用
WHERE B.ntitle= 2来查询
想了好几天了,可是还无法解决
请帮帮加我一下好吗?QQ:252377274
也可以告诉你的QQ,我来加一下谢谢