问个简单的问题,顺便散点儿分...

喜-喜 2010-05-02 07:21:08
'需求内容:'

假设现在有两个表,一个是表T1,一个表T2
录入的数据如下:
T1
id name
---------------------------
1 wang;huang;yan
2 li;cai;tang
3 zhang;chen
4 song;gao

T2
name property
---------------------------
wang 上海;北京
li 深圳
zhang 广州
song 厦门;唐山;西安

T1中的name列及T2中的property不能再拆分,如何构造语句来实现通过查询T2表中的property中的广州来得到T1表中id的值?



请问下面的解法是否正确?
charindex 的用法是否正确?
为什我在SQL Server 2005 环境下执行不出结果...


use test
go
if object_id('test.dbo.t1') is not null drop table t1
-- 创建数据表
create table t1
(
id int,
name char(20)
)
go
--插入测试数据
insert into t1 select 1,'wang;huang;yan'
union all select 2,'li;cai;tang'
union all select 3,'zhang;chen'
union all select 4,'song;gao'
go
if object_id('test.dbo.t2') is not null drop table t2
-- 创建数据表
create table t2
(
name char(10),
property char(20)
)
go
--插入测试数据
insert into t2 select 'wang','上海;北京'
union all select 'li','深圳'
union all select 'zhang','广州'
union all select 'song','厦门;唐山;西安'
union all select 'li','广州;深圳'
go
--代码实现

'解法一'
select * from t1 t where
exists(select 1 from t2
where charindex(';'+name+';',';'+t.name+';')>0 and [property] like '%广州%')

'解法二'
select t1.* from t1 , t2
where charindex(';'+t2.name + ';',';'+t1.name+';') > 0 and charindex(';广州;',';'+t2.property+';') > 0
order by t1.id



charindex(';'+name+';',';'+t.name+';')>0 这样的写法能实现么??
好像 charindex 函数里的第一个表达式必须是定值,不应该包含字段变量吧...
郁闷...



...全文
150 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinqing123111 2012-08-14
  • 打赏
  • 举报
回复
过来混点儿分的。不要管我。
喜-喜 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 feixianxxx 的回复:]
引用 17 楼 bancxc 的回复:

一般不要用char 会自动加空格
用nvarchar 或varchar

长度比较稳定的字段属性就应该使用char nchar...
[/Quote]

恩!晓得了....

多谢各位朋友的热心帮助....

结贴给分...
feixianxxx 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 bancxc 的回复:]

一般不要用char 会自动加空格
用nvarchar 或varchar
[/Quote]
长度比较稳定的字段属性就应该使用char nchar...
bancxc 2010-05-02
  • 打赏
  • 举报
回复
一般不要用char 会自动加空格
用nvarchar 或varchar
喜-喜 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sql77 的回复:]
引用 13 楼 happycell188 的回复:
引用 5 楼 sql77 的回复:
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR


确实是这样的原因啊!但又来问题了...


SQL code
'两表的 name 为 varchar ,porperty 为 cha……
[/Quote]

哦...
feixianxxx 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 happycell188 的回复:]

引用 5 楼 sql77 的回复:
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR


确实是这样的原因啊!但又来问题了...

SQL code
'两表的 name 为 varchar ,porperty 为 char 时'
第一种解法可以得出想要的结果:
id ……
[/Quote]
都是一样的原因....
SQL77 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 happycell188 的回复:]
引用 5 楼 sql77 的回复:
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR


确实是这样的原因啊!但又来问题了...


SQL code
'两表的 name 为 varchar ,porperty 为 char 时'
第一种解法可以得出想要的结果:
id ……
[/Quote]
楼主还是先去理解CHAR和VARCHAR吧,
喜-喜 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sql77 的回复:]
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR
[/Quote]

确实是这样的原因啊!但又来问题了...

'两表的 name 为 varchar ,porperty 为 char 时'
第一种解法可以得出想要的结果:
id name
------------------------
2 li;cai;tang
3 zhang;chen

而第二种解法却是这样的结果:
id name
-------------------------
2 li;cai;tang


'当表中的所有字符型字段均为 varchar 时'
两种解法均能够得到相同的结果!!
均为:
id name
------------------------
2 li;cai;tang
3 zhang;chen

'这是什么原因啊????'
feixianxxx 2010-05-02
  • 打赏
  • 举报
回复
.....把补的空格用RTRIM()去掉。。
envykok 2010-05-02
  • 打赏
  • 举报
回复


SELECT * FROM t1 t
WHERE
exists
(SELECT name from t2 where property like '%广州%' and CHARINDEX(rtrim(ltrim(name)),t.name)>0)
gw6328 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sql77 的回复:]
SQL code
if object_id('dbo.t1') is not null drop table t1
-- 创建数据表
create table t1
(
id int,
name varchar(20)
)
go
--插入测试数据
insert into t1 select 1,'wang;huang;yan'
union all select 2,'li;c……
[/Quote]
哦。是的。那个函数据的参数要varchar
SQL77 2010-05-02
  • 打赏
  • 举报
回复
if object_id('dbo.t1') is not null drop table t1
-- 创建数据表
create table t1
(
id int,
name varchar(20)
)
go
--插入测试数据
insert into t1 select 1,'wang;huang;yan'
union all select 2,'li;cai;tang'
union all select 3,'zhang;chen'
union all select 4,'song;gao'
go
if object_id('dbo.t2') is not null drop table t2
-- 创建数据表
create table t2
(
name varchar(10),
property varchar(20)
)
go
--插入测试数据
insert into t2 select 'wang','上海;北京'
union all select 'li','深圳'
union all select 'zhang','广州'
union all select 'song','厦门;唐山;西安'
union all select 'li','广州;深圳'
go
--代码实现

--'解法一'
select * from t1 t where
exists(select 1 from t2
where charindex(';'+name+';',';'+t.name+';')>0 and [property] like '%广州%')
/*

(4 行受影响)

(5 行受影响)
id name
----------- --------------------
2 li;cai;tang
3 zhang;chen

(2 行受影响)

*/
gw6328 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 happycell188 的回复:]
引用 3 楼 jinfengyiye 的回复:
为什么加上 ltrim(rtrim())就行了?


1 楼的的确把 charindex 的第一项设定为了定值,但是他的结果应该固定只有一条,也就是当 T2 中的数据为

insert into t2 select 'wang','上海;北京'
union all select 'li','深圳'
union all select '……
[/Quote]
是两条
select * from t1 t where
exists(select 1 from t2
where charindex(ltrim(rtrim(t2.name)),t.name)>0 and [property] like '%广州%')
------------------------
2 li;cai;tang
3 zhang;chen
喜-喜 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jinfengyiye 的回复:]
为什么加上 ltrim(rtrim())就行了?
[/Quote]

1 楼的的确把 charindex 的第一项设定为了定值,但是他的结果应该固定只有一条,也就是当 T2 中的数据为

insert into t2 select 'wang','上海;北京'
union all select 'li','深圳'
union all select 'zhang','广州'
union all select 'song','厦门;唐山;西安'
union all select 'li','广州;深圳'

时,只能取出 name 为 'zhang' 的那条记录...
gw6328 2010-05-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sql77 的回复:]
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR
[/Quote]
改成varchar了还是不行。
SQL77 2010-05-02
  • 打赏
  • 举报
回复
create table t2
(
name char(10),
property char(20)
)

原来你的表有问题的说,字段是CHAR
SQL77 2010-05-02
  • 打赏
  • 举报
回复
为什我在SQL Server 2005 环境下执行不出结果...

我的都是05,呵呵

select t1.*,charindex(';'+t2.name + ';',';'+t1.name+';')
from t1 , t2
where charindex(';'+t2.name + ';',';'+t1.name+';') > 0 and charindex(';广州;',';'+t2.property+';') > 0
order by t1.id


这样总得看清楚了吧,你可以把它当作定值来看待
gw6328 2010-05-02
  • 打赏
  • 举报
回复
为什么加上 ltrim(rtrim())就行了?
liangCK 2010-05-02
  • 打赏
  • 举报
回复
varchar
envykok 2010-05-02
  • 打赏
  • 举报
回复


SELECT * FROM t1
WHERE CHARINDEX(RTRIM(LTRIM((SELECT top 1 name from t2 where property='广州'))),name)>0

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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