我在SQL Server有2个表。一个保存字段的名称,另一个保存id的组合。我对更高级的SQL查询有点陌生,并且很难找到一种好方法来实现这一点。如果我使用javascript,我只需将每个数据地址拆分为一个数组,并循环每个数组以获得所需的输出。不知道如何在SQL中完成。
t1
请尝试以下解决方案。
/p>-- DDL and sample data population, start
DECLARE @tbl1 TABLE (id INT PRIMARY KEY, name VARCHAR(20));
INSERT @tbl1 (id, name) VALUES
(0, 'Manager'),
(1, 'Client'),
(2, 'FirstName'),
(3, 'LastName');
DECLARE @tbl2 TABLE (dataaddress VARCHAR(20));
INSERT @tbl2 (dataaddress) VALUES
('0.2'),
('0.3'),
('1.2'),
('1.3');
-- DDL and sample data population, end
;WITH rs AS
(
SELECT t1_id = PARSENAME(dataaddress,2)
, t2_id = PARSENAME(dataaddress,1)
FROM @tbl2
)
SELECT addressname = CONCAT(t1.name, '.' , t2.name)
FROM @tbl1 AS t1 INNER JOIN rs ON t1.id = rs.t1_id
INNER JOIN @tbl1 AS t2 ON t2.id = rs.t2_id;
tbody> <<tr>addressname 经理。FirstName 经理。LastName 客户端。FirstName 客户端。LastName