【发布时间】:2016-08-18 19:49:35
【问题描述】:
有类似的数据:
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..) 例如我只写了其中的两个
【问题讨论】:
-
使用 Joins 和 Group By 无法实现您想要的输出。尝试使用循环。
-
@Luftwaffe 你能举个例子吗?我不知道该怎么做。我还编辑了问题。没必要怎么做
-
你想输出和上面一样的格式吗?
-
@Luftwaffe 在我将连接这个字符串之后。现在我只需要有问题的结果
标签: sql-server sql-server-2008 tsql