是这样吗:
create table 题目表(题目ID int, 题目内容 nvarchar(30))insert into 题目表
select 1 ,'这是一个题目'
create table 知识点表(知识点ID int, 题目ID int, 知识点 nvarchar(30))insert into 知识点表
select 1 , 1 , '数据结构'
union all select 2 , 1 , '算法'
union all select 3, 1, '递归'我想要查出的数据是这个样子的
题目ID 题目内容 知识点
1 这是一个题目 数据结构,算法,递归
select 题目ID,题目内容,
STUFF(
(
select ',' + 知识点
from 知识点表 t2
where t1.题目ID = t2.题目ID
order by 知识点ID
for xml path('')
),
1,1,''
)
from 题目表 t1
/*
题目ID 题目内容 知识点
1 这是一个题目 数据结构,数据结构,算法,递归
*/
create table 题目表(题目ID int, 题目内容 nvarchar(30))insert into 题目表
select 1 ,'这是一个题目'
create table 知识点表(知识点ID int, 题目ID int, 知识点 nvarchar(30))insert into 知识点表
select 1 , 1 , '数据结构'
union all select 2 , 1 , '算法'
union all select 3, 1, '递归'我想要查出的数据是这个样子的
题目ID 题目内容 知识点
1 这是一个题目 数据结构,算法,递归
select 题目ID,题目内容,
STUFF(
(
select ',' + 知识点
from 知识点表 t2
where t1.题目ID = t2.题目ID
order by 知识点ID
for xml path('')
),
1,1,''
)
from 题目表 t1
/*
题目ID 题目内容 知识点
1 这是一个题目 数据结构,数据结构,算法,递归
*/
create table 题目表(题目ID int, 题目内容 nvarchar(30))insert into 题目表
select 1 ,'这是一个题目'
create table 知识点表(知识点ID int, 题目ID int, 知识点 nvarchar(30))insert into 知识点表
select 1 , 1 , '数据结构'
union all select 2 , 1 , '算法'
union all select 3, 1, '递归'
select 题目ID,题目内容,
STUFF(
(
select ',' + 知识点
from 知识点表 t2
where t1.题目ID = t2.题目ID
order by 知识点ID
for xml path('')
),
1,1,''
)
from 题目表 t1
/*
题目ID 题目内容 知识点
1 这是一个题目 数据结构,数据结构,算法,递归
*/
再改一下:
create table 题目表(题目ID int, 题目内容 nvarchar(30))insert into 题目表
select 1 ,'这是一个题目'
create table 知识点表(知识点ID int, 题目ID int, 知识点 nvarchar(30))insert into 知识点表
select 1 , 1 , '数据结构'
union all select 2 , 1 , '算法'
union all select 3, 1, '递归'
select distinct 题目ID,题目内容,
STUFF(
(
select ',' + 知识点
from 知识点表 t2
where t1.题目ID = t2.题目ID
order by 知识点ID
for xml path('')
),
1,1,''
) as 知识点
from 题目表 t1
/*
题目ID 题目内容 知识点
1 这是一个题目 数据结构,算法,递归
*/
这个没关系吧 这是sql标准语法都可以的。结贴了。谢了。
这个没关系吧 这是sql标准语法都可以的。结贴了。谢了。哦 是标准sql语法,但是有些特定的,是只针对某个版本的哈
这个没关系吧 这是sql标准语法都可以的。结贴了。谢了。我加你关注了哈,要是上面的代码,在你的sql server中不能用,就给我发私信,我到时候再帮你看看