一句SQL语句不会写

时间:2021-03-20 15:14:43
关于信息管理的几张表
首先是信息的栏目column表,
c_id(栏目id)  c_name(栏目名)  parent_id(父栏id)  have_son(1有子栏)  mapping
  3             体育             1                 1                 null
  4          体育信息            3                 1                 null
  5          足球信息            4                 0                 null
  6        体育记者信息          3                 0               reporter
信息存放的info表
auto_id   c_id(信息所属的栏目id号)  title(信息标题)  content(信息内容)
   1        5                        AC米兰1:0切沃   西多夫打进制胜一球

reporter表
auto_id   c_id   r_name  r_age  r_sex
   1        6     记者A   34      m

因为不是所有的栏目里的信息都是由信息标题,信息内容等基本字段组成,所以如果有特殊字段的栏目就不能放在基本信息表里,mapping字段如果是null时表示该栏目里的信息是普通信息,并且信息都放在info表里,如果不是null,例如记者信息栏目,该mapping字段等于reporter,表示记者信息都放在reporter表里。

我的需求如下:
当我点体育栏目时,将会显示所有体育子栏目里的信息标题(记者名字当作信息标题),也就是如下结果

auto_id   c_id             title(信息标题)  
   1        5               AC米兰1:0切沃  
   1        6                记者A

22 个解决方案

#1


樓主把你的表結構和數據也貼出來把。。。方便測試...
寫成這樣
create table columns
(a int primary..)
insert into columns select '','',''

#2


有点儿看晕了,不知道楼主在说什么

#3


楼主数据给错了8?
c_id(栏目id)  c_name(栏目名)  parent_id(父栏id)  have_son(1有子栏)  mapping
  3             体育             1                 1                 null
  4          体育信息            3                 1                 null
  5          足球信息            4                 0                 null
  6        体育记者信息          3                 0               reporter

体育的子栏是 体育信息和足球信息分别是4,6不是5,6啊!


信息存放的info表  ,应该是这样的吧?
auto_id   c_id(信息所属的栏目id号)  title(信息标题)  content(信息内容)
   1        4                        AC米兰1:0切沃   西多夫打进制胜一球


用union

sql:

   select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b where parent_id=3 and a.mapping is null and a.c_id=b.c_id union select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, reporter表 b where parent_id=3 and a.mapping =
'reporter' and a.c_id=b.c_id

#4


体育是一级栏目,其下2级栏目有体育信息和体育记者信息,体育信息下又有3级栏目足球信息,我觉得很清晰的,大家再仔细看看。

#5


忘了还要自己连一次:

select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b,信息的栏表 c where parent_id=3 and a.mapping is null and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' union select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, reporter表 b,信息的栏表 c where parent_id=3 and a.mapping =
'reporter' and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' 

#6


select  b.auto_id , a.c_id  ,b.title from  column a left join (select auto_id,c_id,title from info union all select auto_id,c_id,r_name as title from reporter )b on a.c_id=b.c_id
where a.c_id!='3'and a.cid!='1'

#7


从楼上的几个sql上也能看出来楼主写的东西很有歧异啊,费解ing

#8


select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b,信息的栏表 c where parent_id=3 and a.mapping is null and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' union select b.auto_id, a.c_id, b.r_name title from 信息的栏表 a, reporter表 b,信息的栏表 c where parent_id=3 and a.mapping ='reporter' and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' 

郁闷

#9


大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
如果mapping字段为空,那么该栏目里的信息应该从info表里查,
如果mapping不为空,那么该栏目里的信息应该在mapping字段映射的表里

#10


楼主,你的column表的分级最多会有几级??还是不限制级别

#11


应该不会很多级,不超过10级

#12


帮顶

#13


知道该从reporter表取信息,那怎么知道取那个字段?

#14


只要有映射的表,默认是第3个字段

#15


默认第三个字段,那岂不是还得查系统表来取字段名,简单是麻烦

#16


auto_id字段的值呢?

#17


在最终查询结果里auto_id可能会重复,但是还有module_id加以区分

#18


这表是别人设计的,我现在要从里面查询也是觉得很麻烦

#19


当查一个栏目的信息时,首先判断该栏目的mapping字段,如果为空就默认查info表,如果不为空就查mapping里映射的表名(只查前3个字段,auto_id,column_id,title)
可以把r_name改成title以作统一

#20


--两层处理,一层是处理搜索指定结点的所有子结点,另一层处理是 mapping 非null时,动态关联表

--测试数据
create table [column](c_id int,c_name nvarchar(10),parent_id int,have_son bit,mapping nvarchar(255))
insert [column] select 3,N'体育'        ,1 ,1,null
union  all      select 4,N'体育信息'    ,3 ,1,null
union  all      select 5,N'足球信息'    ,4 ,0,null
union  all      select 6,N'体育记者信息',5 ,0,N'reporter'
union  all      select 7,N'体育组织信息',5 ,0,N'abc'
union  all      select 8,N'体育组织信息',6 ,0,N'abcd'

create table info(auto_id int,c_id int,title nvarchar(10),content nvarchar(100))
insert info select 1,5,N'AC米兰1:0切沃',N'西多夫打进制胜一球'

create table reporter(auto_id int,c_id int,r_name nvarchar(10),r_age int,r_sex char(1))
insert reporter select 1,6,N'记者A',34,'m'

create table abc(auto_id int,c_id int,c_name nvarchar(10),r_age int,r_sex char(1))
insert abc select 1,7,N'组织A',34,'m'
go

--查询处理的存储过程
create proc p_qry
@c_name nvarchar(10)  --要查询的栏目名称
as
set nocount on
create table #(c_id int,level int,mapping nvarchar(255))
declare @l int
set @l=0
insert # select c_id,@l,mapping
from [column]
where c_name=@c_name
while @@rowcount>0
begin
set @l=@l+1
insert # select a.c_id,@l,a.mapping
from [column] a,# b
where a.parent_id=b.c_id and b.level=@l-1
end
declare @s nvarchar(4000)
set @s=''
select @s=@s+N'
union all
select a.*,b.'+quotename(c.name)+N' from [column] a,'+quotename(o.name)+N' b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping=N'+quotename(o.name,N'''')
from # a,sysobjects o,syscolumns c
where a.mapping>''
and a.mapping=o.name
and o.id=c.id
and c.colid=3
exec(N'
select a.*,b.title 
from [column] a,info b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping is null'+@s)
go

--调用存储过程实现查询
exec p_qry N'体育信息'
go
--删除测试
drop table [column],info,reporter,abc
drop proc p_qry

/*--测试结果

c_id        c_name     parent_id   have_son mapping    title      
----------- ---------- ----------- -------- -----------------------------
5           足球信息       4           0        NULL       AC米兰1:0切沃
6           体育记者信息     5           0        reporter   记者A
7           体育组织信息     5           0        abc        组织A
--*/

#21


--两层处理,一层是处理搜索指定结点的所有子结点,另一层处理是 mapping 非null时,动态关联表

--测试数据
create table [column](c_id int,c_name nvarchar(10),parent_id int,have_son bit,mapping nvarchar(255))
insert [column] select 3,N'体育'        ,1 ,1,null
union  all      select 4,N'体育信息'    ,3 ,1,null
union  all      select 5,N'足球信息'    ,4 ,0,null
union  all      select 6,N'体育记者信息',5 ,0,N'reporter'
union  all      select 7,N'体育组织信息',5 ,0,N'abc'
union  all      select 8,N'体育组织信息',6 ,0,N'abcd'

create table info(auto_id int,c_id int,title nvarchar(10),content nvarchar(100))
insert info select 1,5,N'AC米兰1:0切沃',N'西多夫打进制胜一球'

create table reporter(auto_id int,c_id int,r_name nvarchar(10),r_age int,r_sex char(1))
insert reporter select 1,6,N'记者A',34,'m'

create table abc(auto_id int,c_id int,c_name nvarchar(10),r_age int,r_sex char(1))
insert abc select 1,7,N'组织A',34,'m'
go

--查询处理的存储过程
create proc p_qry
@c_name nvarchar(10)  --要查询的栏目名称
as
set nocount on
create table #(c_id int,level int,mapping nvarchar(255))
declare @l int
set @l=0
insert # select c_id,@l,mapping
from [column]
where c_name=@c_name
while @@rowcount>0
begin
set @l=@l+1
insert # select a.c_id,@l,a.mapping
from [column] a,# b
where a.parent_id=b.c_id and b.level=@l-1
end
declare @s nvarchar(4000)
set @s=''
select @s=@s+N'
union all
select a.c_id,a.c_name,b.auto_id,b.'+quotename(c.name)+N' from [column] a,'+quotename(o.name)+N' b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping=N'+quotename(o.name,N'''')
from # a,sysobjects o,syscolumns c
where a.mapping>''
and a.mapping=o.name
and o.id=c.id
and c.colid=3
exec(N'
select a.c_id,a.c_name,b.auto_id,b.title 
from [column] a,info b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping is null'+@s)
go

--调用存储过程实现查询
exec p_qry N'体育信息'
go
--删除测试
drop table [column],info,reporter,abc
drop proc p_qry

/*--测试结果

c_id        c_name     auto_id     title      
----------- ---------- ----------- ---------- 
5           足球信息       1           AC米兰1:0切沃
6           体育记者信息     1           记者A
7           体育组织信息     1           组织A
--*/

#22


太感谢了

#1


樓主把你的表結構和數據也貼出來把。。。方便測試...
寫成這樣
create table columns
(a int primary..)
insert into columns select '','',''

#2


有点儿看晕了,不知道楼主在说什么

#3


楼主数据给错了8?
c_id(栏目id)  c_name(栏目名)  parent_id(父栏id)  have_son(1有子栏)  mapping
  3             体育             1                 1                 null
  4          体育信息            3                 1                 null
  5          足球信息            4                 0                 null
  6        体育记者信息          3                 0               reporter

体育的子栏是 体育信息和足球信息分别是4,6不是5,6啊!


信息存放的info表  ,应该是这样的吧?
auto_id   c_id(信息所属的栏目id号)  title(信息标题)  content(信息内容)
   1        4                        AC米兰1:0切沃   西多夫打进制胜一球


用union

sql:

   select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b where parent_id=3 and a.mapping is null and a.c_id=b.c_id union select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, reporter表 b where parent_id=3 and a.mapping =
'reporter' and a.c_id=b.c_id

#4


体育是一级栏目,其下2级栏目有体育信息和体育记者信息,体育信息下又有3级栏目足球信息,我觉得很清晰的,大家再仔细看看。

#5


忘了还要自己连一次:

select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b,信息的栏表 c where parent_id=3 and a.mapping is null and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' union select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, reporter表 b,信息的栏表 c where parent_id=3 and a.mapping =
'reporter' and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' 

#6


select  b.auto_id , a.c_id  ,b.title from  column a left join (select auto_id,c_id,title from info union all select auto_id,c_id,r_name as title from reporter )b on a.c_id=b.c_id
where a.c_id!='3'and a.cid!='1'

#7


从楼上的几个sql上也能看出来楼主写的东西很有歧异啊,费解ing

#8


select b.auto_id, a.c_id, b.title(信息标题) from 信息的栏表 a, info表 b,信息的栏表 c where parent_id=3 and a.mapping is null and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' union select b.auto_id, a.c_id, b.r_name title from 信息的栏表 a, reporter表 b,信息的栏表 c where parent_id=3 and a.mapping ='reporter' and a.c_id=b.c_id and a.parent_id=c.c_id and c.c_name='体育' 

郁闷

#9


大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
大家写的都有问题,肯定应该有if判断
如果mapping字段为空,那么该栏目里的信息应该从info表里查,
如果mapping不为空,那么该栏目里的信息应该在mapping字段映射的表里

#10


楼主,你的column表的分级最多会有几级??还是不限制级别

#11


应该不会很多级,不超过10级

#12


帮顶

#13


知道该从reporter表取信息,那怎么知道取那个字段?

#14


只要有映射的表,默认是第3个字段

#15


默认第三个字段,那岂不是还得查系统表来取字段名,简单是麻烦

#16


auto_id字段的值呢?

#17


在最终查询结果里auto_id可能会重复,但是还有module_id加以区分

#18


这表是别人设计的,我现在要从里面查询也是觉得很麻烦

#19


当查一个栏目的信息时,首先判断该栏目的mapping字段,如果为空就默认查info表,如果不为空就查mapping里映射的表名(只查前3个字段,auto_id,column_id,title)
可以把r_name改成title以作统一

#20


--两层处理,一层是处理搜索指定结点的所有子结点,另一层处理是 mapping 非null时,动态关联表

--测试数据
create table [column](c_id int,c_name nvarchar(10),parent_id int,have_son bit,mapping nvarchar(255))
insert [column] select 3,N'体育'        ,1 ,1,null
union  all      select 4,N'体育信息'    ,3 ,1,null
union  all      select 5,N'足球信息'    ,4 ,0,null
union  all      select 6,N'体育记者信息',5 ,0,N'reporter'
union  all      select 7,N'体育组织信息',5 ,0,N'abc'
union  all      select 8,N'体育组织信息',6 ,0,N'abcd'

create table info(auto_id int,c_id int,title nvarchar(10),content nvarchar(100))
insert info select 1,5,N'AC米兰1:0切沃',N'西多夫打进制胜一球'

create table reporter(auto_id int,c_id int,r_name nvarchar(10),r_age int,r_sex char(1))
insert reporter select 1,6,N'记者A',34,'m'

create table abc(auto_id int,c_id int,c_name nvarchar(10),r_age int,r_sex char(1))
insert abc select 1,7,N'组织A',34,'m'
go

--查询处理的存储过程
create proc p_qry
@c_name nvarchar(10)  --要查询的栏目名称
as
set nocount on
create table #(c_id int,level int,mapping nvarchar(255))
declare @l int
set @l=0
insert # select c_id,@l,mapping
from [column]
where c_name=@c_name
while @@rowcount>0
begin
set @l=@l+1
insert # select a.c_id,@l,a.mapping
from [column] a,# b
where a.parent_id=b.c_id and b.level=@l-1
end
declare @s nvarchar(4000)
set @s=''
select @s=@s+N'
union all
select a.*,b.'+quotename(c.name)+N' from [column] a,'+quotename(o.name)+N' b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping=N'+quotename(o.name,N'''')
from # a,sysobjects o,syscolumns c
where a.mapping>''
and a.mapping=o.name
and o.id=c.id
and c.colid=3
exec(N'
select a.*,b.title 
from [column] a,info b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping is null'+@s)
go

--调用存储过程实现查询
exec p_qry N'体育信息'
go
--删除测试
drop table [column],info,reporter,abc
drop proc p_qry

/*--测试结果

c_id        c_name     parent_id   have_son mapping    title      
----------- ---------- ----------- -------- -----------------------------
5           足球信息       4           0        NULL       AC米兰1:0切沃
6           体育记者信息     5           0        reporter   记者A
7           体育组织信息     5           0        abc        组织A
--*/

#21


--两层处理,一层是处理搜索指定结点的所有子结点,另一层处理是 mapping 非null时,动态关联表

--测试数据
create table [column](c_id int,c_name nvarchar(10),parent_id int,have_son bit,mapping nvarchar(255))
insert [column] select 3,N'体育'        ,1 ,1,null
union  all      select 4,N'体育信息'    ,3 ,1,null
union  all      select 5,N'足球信息'    ,4 ,0,null
union  all      select 6,N'体育记者信息',5 ,0,N'reporter'
union  all      select 7,N'体育组织信息',5 ,0,N'abc'
union  all      select 8,N'体育组织信息',6 ,0,N'abcd'

create table info(auto_id int,c_id int,title nvarchar(10),content nvarchar(100))
insert info select 1,5,N'AC米兰1:0切沃',N'西多夫打进制胜一球'

create table reporter(auto_id int,c_id int,r_name nvarchar(10),r_age int,r_sex char(1))
insert reporter select 1,6,N'记者A',34,'m'

create table abc(auto_id int,c_id int,c_name nvarchar(10),r_age int,r_sex char(1))
insert abc select 1,7,N'组织A',34,'m'
go

--查询处理的存储过程
create proc p_qry
@c_name nvarchar(10)  --要查询的栏目名称
as
set nocount on
create table #(c_id int,level int,mapping nvarchar(255))
declare @l int
set @l=0
insert # select c_id,@l,mapping
from [column]
where c_name=@c_name
while @@rowcount>0
begin
set @l=@l+1
insert # select a.c_id,@l,a.mapping
from [column] a,# b
where a.parent_id=b.c_id and b.level=@l-1
end
declare @s nvarchar(4000)
set @s=''
select @s=@s+N'
union all
select a.c_id,a.c_name,b.auto_id,b.'+quotename(c.name)+N' from [column] a,'+quotename(o.name)+N' b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping=N'+quotename(o.name,N'''')
from # a,sysobjects o,syscolumns c
where a.mapping>''
and a.mapping=o.name
and o.id=c.id
and c.colid=3
exec(N'
select a.c_id,a.c_name,b.auto_id,b.title 
from [column] a,info b,# b1
where a.c_id=b1.c_id and b.c_id=b1.c_id and b1.mapping is null'+@s)
go

--调用存储过程实现查询
exec p_qry N'体育信息'
go
--删除测试
drop table [column],info,reporter,abc
drop proc p_qry

/*--测试结果

c_id        c_name     auto_id     title      
----------- ---------- ----------- ---------- 
5           足球信息       1           AC米兰1:0切沃
6           体育记者信息     1           记者A
7           体育组织信息     1           组织A
--*/

#22


太感谢了