如前文所述ID, ParentID, Title 实现一种子母结构,即如果是父条目,ParentID为0,如果是子条目,ParentID=父条目ID 现在要取出所有 父条目和第一个子条目的title 即返回这样两列 ParentTitle,SonTitle源表第2条记录的ParentID指向其母记录,于是就把2和1合并 同样把3和4合并起来
看一下是不是这样的create table ori (id int ,parentid int ,title varchar (50))insert into ori select 1,0,'"Question 1"' union select 2,1,'"Answer 1"' union select 3,0,'"Question 2"' union select 4,3,'"Answer 2"'select a.title as Quesiont,b.title AS Answer FROM (select a=right(title,charindex(' ',reverse(title))-1),title from ori where title like '%Question%') a, (select a=right(title,charindex(' ',reverse(title))-1),title from ori where title like '%Answer%') b where a.a=b.a
create table ori (id int ,pid int ,title varchar (50))insert into ori select 1,0,'"Question 1"' union select 2,1,'"Answer 1"' union select 3,0,'"Question 2"' union select 4,3,'"Answer 2"'select a.title,b.title from ori a inner join ori b on a.id=b.piddrop table ori
ID, ParentID, Title
实现一种子母结构,即如果是父条目,ParentID为0,如果是子条目,ParentID=父条目ID
现在要取出所有 父条目和第一个子条目的title
即返回这样两列
ParentTitle,SonTitle
如何实现?
但是每个问题只有一个回答
现在要把一个提问和对应的回答取出来放入一个表
也就是原来是同一个字段的Title值,现在放在两个字段里了
--------------------------
ID ParentID Title
1 0 "Question 1"
2 1 "Answer 1"
3 0 "Question 2"
4 3 "Answer 2"
---------------------------
要输出
Quesiont & Answer Tabel
--------------------------
Question Answer
"Question 1" "Answer 1"
"Question 2" "Answer 2"
-----------------------------
实现一种子母结构,即如果是父条目,ParentID为0,如果是子条目,ParentID=父条目ID
现在要取出所有 父条目和第一个子条目的title
即返回这样两列
ParentTitle,SonTitle源表第2条记录的ParentID指向其母记录,于是就把2和1合并
同样把3和4合并起来
select 1,0,'"Question 1"' union
select 2,1,'"Answer 1"' union
select 3,0,'"Question 2"' union
select 4,3,'"Answer 2"'select a.title as Quesiont,b.title AS Answer
FROM
(select a=right(title,charindex(' ',reverse(title))-1),title from ori where title like '%Question%') a,
(select a=right(title,charindex(' ',reverse(title))-1),title from ori where title like '%Answer%') b
where a.a=b.a
我的意思。这个里面不一定是"question""answer"这些字符的,但是是这样的关系,我要把它取出来
select 1,0,'"Question 1"' union
select 2,1,'"Answer 1"' union
select 3,0,'"Question 2"' union
select 4,3,'"Answer 2"'select a.title,b.title from ori a inner join ori b on a.id=b.piddrop table ori