拥有以下数据:
declare @test_names TABLE(id int identity(1,1), name character varying(50),age int);
INSERT INTO @test_names(name,age) values ('name1',10),('name2',20);
declare @test_names_details TABLE(id int identity(1,1), test_names_id int,col1 int,col2 int,col3 int);
INSERT INTO @test_names_details(test_names_id,col1,col2,col3)
VALUES(1,2,3,4),(1,5,6,7),(1,8,9,10),(2,20,21,22),(2,23,24,25);
想要从第二个表中选择第一个表值的详细信息。怎么做 ?输出必须如下:
field1 field2 field3
name1 10
2 3 4
5 6 7
8 9 10
name2 20
20 21 22
23 24 25
被修改
在表格中我有很多行(name1,name2,name3 ..),例如我只写了两行
答案 0 :(得分:1)
DECLARE @output TABLE(field1 VARCHAR(50), field2 VARCHAR(50), field3 VARCHAR(50))
DECLARE @test_names_count INT,
@counter INT
SELECT @test_names_count = COUNT(1) FROM @test_names
SET @counter = 1
WHILE (@counter <= @test_names_count)
BEGIN
INSERT INTO @output SELECT name, age, '' FROM @test_names WHERE id = @counter
INSERT INTO @output SELECT col1, col2, col3 FROM @test_names_details WHERE test_names_id = @counter
SET @counter = @counter + 1
END
SELECT * FROM @output
答案 1 :(得分:0)
针对任意数量的名称列进行了更新:
基于设置的解决方案,将列名替换为您选择的名称
Dim toString As String
toString = cell.Value & "_"
If (InStr(toString, ",")) Then
toString = Replace(toString, ",", ".")
toString = Trim(toString)
cell.Value = " " + Left(toString, (Len(toString) - 1))
End If