现在有A,B两个表,
A表中有4个字段,a_id,b_id,c_id,d_id,
B表中有两个字段,id,name
需求,A表中的4个id都是B表中的id,现在查A表,需要把B表的名字同时查询出来,翻译
思路,用A表joinB表,可以实现需求,但是感觉数据量变大后效率太低
各位有什么其它好的解决思路。最好是pssql。
建立索引或者增加冗余字段。
我是这么做查询转换的。仅供参考。
ALTER function [dbo].[GetDictionaryValue](@in nvarchar(255))
returns nvarchar(255)
as
begin
-- 先决条件:D_Code组合必须是用','隔开
-- 函数用途:可以将DictionaryValue表中的 任意D_Code字段 转化 成 D_Name 并使用逗号分割开。
declare @out nvarchar(255) = '';
declare @index int;
declare @name nvarchar(255) = '';
declare @code nvarchar(255);
while(LEN(@in)>0)
begin
select @index = CHARINDEX(',', @in, 0); --找到第一个逗号
if(@index>0)
begin
set @code =Left(@in,@index-1);
end;
else
begin
set @code = @in;
set @index = LEN(@in);
end;
set @in = RIGHT(@in,LEN(@in)-@index); --剩余部分
select @name = D_Name from DictionaryValue where D_Code = @code;
if(LEN(@out)=0)
set @out =@out + @name;
else
set @out =@out +','+ @name;
end;
return @out;
end;