不显示删除回复显示所有回复显示星级回复显示得分回复 数据库某字段为字典编码的多个代码,如何通过查询语句转换为中文

xuhesheng 2010-10-21 01:51:10
问题描述
数据库某表的某个字段,存放的是某种字典编码
但是由于是多选框选择的内容
所以存放的内容比如说是[A1,A3,A5]

另一张字典表B里的映射关系是
A1----中国
A2----美国
A3----日本
A4----韩国
A5----朝鲜
A6...等等

问题是
如何能够让我的查询语句直接查询出的结果是[中国,日本,朝鲜]
...全文
144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuhesheng 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 abuying 的回复:]
引用 8 楼 xuhesheng 的回复:

大家没明白

如果我这个字段和其他的字段需要一起被查询出来呢
你有什么办法么

那就加一个

SQL code
union --去掉重复
select * from tb
[/Quote]
看来你不是很理解字典表
这种方案下
你只能对于只查这个字段的问题有效
如果我同时查这个字段和其他普通字段你的union岂不是要累赘着写很多
abuying 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xuhesheng 的回复:]

大家没明白

如果我这个字段和其他的字段需要一起被查询出来呢
你有什么办法么
[/Quote]
那就加一个
union --去掉重复 
select * from tb
xuhesheng 2010-10-22
  • 打赏
  • 举报
回复
大家没明白

如果我这个字段和其他的字段需要一起被查询出来呢
你有什么办法么
dawugui 2010-10-21
  • 打赏
  • 举报
回复
更多内容请参考:

/*
标题:分解字符串并查询相关数据
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-03-18
地点:广东深圳
说明:通过使用函数等方法分解字符串查询相关数据。

问题:通过分解一个带某种符号分隔的字符串在数据库中查找相关数据。
例如 @str = '1,2,3',查询下表得到记录1,4,5,6
ID TypeID
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
6 6,7
*/
-----------------------------
create table tb (ID int , TypeID varchar(30))
insert into tb values(1 , '1,2,3,4,5,6,7,8,9,10,11,12')
insert into tb values(2 , '2,3')
insert into tb values(3 , '3,7,8,9')
insert into tb values(4 , '2,6')
insert into tb values(5 , '4,5')
insert into tb values(6 , '6,7')
go
-----------------------------
--如果仅仅是一个,如@str = '1'.
declare @str as varchar(30)
set @str = '1'
select * from tb where charindex(',' + @str + ',' , ',' + TypeID + ',') > 0
select * from tb where ',' + TypeID + ',' like '%,' + @str + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
(所影响的行数为 1 行)
*/

-----------------------------
--如果包含两个,如@str = '1,2'.
declare @str as varchar(30)
set @str = '1,2'
select * from tb where charindex(',' + left(@str , charindex(',' , @str) - 1) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',' , ',' + typeid + ',') > 0
select * from tb where ',' + typeid + ',' like '%,' + left(@str , charindex(',' , @str) - 1) + ',%' or
',' + typeid + ',' like '%,' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
4 2,6
(所影响的行数为 3 行)
*/

-------------------------------------------
--如果包含三个或四个,用PARSENAME函数来处理.
declare @str as varchar(30)
set @str = '1,2,3,4'
select * from tb where
charindex(',' + parsename(replace(@str , ',' , '.') , 4) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 3) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 2) + ',' , ',' + typeid + ',') > 0 or
charindex(',' + parsename(replace(@str , ',' , '.') , 1) + ',' , ',' + typeid + ',') > 0
select * from tb where
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 4) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 3) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 2) + ',%' or
',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 1) + ',%'
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/

---------------------------------------
--如果超过四个,则只能使用函数或动态SQL来分解并查询数据。
/*
名称:fn_split函数.
功能:实现字符串分隔功能的函数
*/
create function dbo.fn_split(@inputstr varchar(8000), @seprator varchar(10))
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator , @inputstr)
while @i >= 1
begin
insert @temp values(left(@inputstr , @i - 1))
set @inputstr = substring(@inputstr , @i + 1 , len(@inputstr) - @i)
set @i = charindex(@seprator , @inputstr)
end
if @inputstr <> '\'
insert @temp values(@inputstr)
return
end
go

--调用
declare @str as varchar(30)
set @str = '1,2,3,4,5'

select distinct m.* from tb m,
(select * from dbo.fn_split(@str,',')) n
where charindex(',' + n.a + ',' , ',' + m.typeid + ',') > 0

drop table tb
drop function dbo.fn_split

/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/

------------------------------------------
--使用动态SQL的语句。
declare @str varchar(200)
declare @sql as varchar(1000)
set @str = '1,2,3,4,5'
set @sql = 'select ''' + replace(@str , ',' , ''' as id union all select ''')
set @sql = @sql + ''''
set @sql = 'select distinct a.* from tb a , (' + @sql + ') b where charindex(' + ''','' + b.id + ' + ''',''' + ' , ' + ''','' + a.typeid + ' + ''',''' + ') > 0 '
exec (@sql)
/*
ID TypeID
----------- ------------------------------
1 1,2,3,4,5,6,7,8,9,10,11,12
2 2,3
3 3,7,8,9
4 2,6
5 4,5
(所影响的行数为 5 行)
*/

dawugui 2010-10-21
  • 打赏
  • 举报
回复
declare @str as varchar(100)
set @str = 'A1,A3,A5'
select * from b where charindex(','+b.col+',',',' + @str + ',') > 0
abuying 2010-10-21
  • 打赏
  • 举报
回复

declare @s varchar(50)
set @s='A1,A3,A5'
select @s
set @s=replace(@s,',','.')
--select @s,parsename(@s,1),parsename(@s,2),parsename(@s,3),parsename(@s,4)
--注此法使用parsename,只能解析类型ip的点分隔,192.168.18.1,最多四段
select * from tb1 where code=parsename(@s,1)
union all
select * from tb1 where code=parsename(@s,2)
union all
select * from tb1 where code=parsename(@s,3)
union all
select * from tb1 where code=parsename(@s,4)
abuying 2010-10-21
  • 打赏
  • 举报
回复

declare @s=varchar(50)
set @s='A1,A3,A5'

select name from tb where id in('',@s)--不知道会 不会错!
claro 2010-10-21
  • 打赏
  • 举报
回复
replace
王向飞 2010-10-21
  • 打赏
  • 举报
回复
字符串分拆,然后再聚合。
alfred_2006 2010-10-21
  • 打赏
  • 举报
回复
感觉很困难阿,要不将第一表的字段存储内容在存成一个1对多表,这样感觉要方便一些,效率也高一些

34,576

社区成员

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

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