SQL数据库里两个‘一对多’的表 怎么绑到gridview里啊,有的数据要合并显示的
比如 表1
ID 姓名
1 张三
2 李四
3 王五表2
RID 科目 分数
1 数学 87
1 语文 78
2 数学 67
2 语文 97要显示成
1 张三 数学87&语文78
2 李四 数学67&语文97
这样怎么弄啊?? 表1对表2是一对多的关系 表2里的数据 相同RID的行是要合并显示的
比如 表1
ID 姓名
1 张三
2 李四
3 王五表2
RID 科目 分数
1 数学 87
1 语文 78
2 数学 67
2 语文 97要显示成
1 张三 数学87&语文78
2 李四 数学67&语文97
这样怎么弄啊?? 表1对表2是一对多的关系 表2里的数据 相同RID的行是要合并显示的
解决方案 »
- 编辑器问题。。。
- HTML select控件的跳转问题~~~请教高手~~~
- 数据库分区怎么分区更合理?寻求高手指点
- 哪里有好点的项目管理软件下载??(最好带有源代码)
- 求asp.net登录源码完全示例 (要vb.net代码)
- 关于FreeTextBox控件! 通过freeTextBox1.Text=dataReader1.GetString(2) ,为什么FreeTextBox显示的并不是完全和当时提交数据库之前的文本
- asp.net web 对话框问题 在线等待
- 簡單的HtmlTable的問題
- 属性对象数组既然可以绑定到 DataGrid.DataSource 中 , 可以赋值到DataSet中吗?
- 在Repeater中如何将数据在多行中的多列显示?
- 今天看到很多网站都是灰色的. 我也想把自己的小站改以下.请问这个代码要怎么改
- 数据集转换成xml的问题,急急~~~
<ItemTemplate>
<%# Eval("id")%> <%# Eval("姓名")%>
<asp:Repeater ID="Repeater2" Runat="server" DataSource='<%# BindSub(Convert.ToInt32(Eval("id"))) %>'>
<ItemTemplate>
<%# Eval("科目")%> <%# Eval("分数")%>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>private void BindMain()
{
SqlConnection cn = new SqlConnection(@"server=.;uid=sa;pwd=;database=Demo");
SqlDataAdapter da = new SqlDataAdapter("select id,姓名 from 表1", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
Repeater1.DataSource = ds.Tables[0].DefaultView;
Repeater1.DataBind();
}protected DataTable BindSub(int id)
{
SqlConnection cn = new SqlConnection(@"server=.;uid=sa;pwd=;database=Demo");
SqlDataAdapter da = new SqlDataAdapter("select 科目, 分数 from 表2 where rid = @id", cn);
da.SelectCommand.Parameters.AddWithValue("id", id);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0];
}protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindMain();
}
}
create table studentInfo
(
sId int,
sName varchar(20)
)
insert into studentInfo([sId],[sName]) values(1,'张三')
insert into studentInfo([sId],[sName]) values(2,'李四')
insert into studentInfo([sId],[sName]) values(3,'王五')select * from [studentInfo]--drop table studentInfocreate table scoreInfo
(
rId int,
pName varchar(20),
score int
)
insert into scoreInfo([rId],[pName],[score]) values(1,'数学',87)
insert into scoreInfo([rId],[pName],[score]) values(1,'语文',78)
insert into scoreInfo([rId],[pName],[score]) values(2,'数学',67)
insert into scoreInfo([rId],[pName],[score]) values(2,'语文',97)select * from scoreInfo
--drop table scoreInfo
select [sId],[sName],(
select
'数学'+ltrim(str(isnull(sum(case [pName] when '数学' then [score] end),0)))
+'&语文'+ltrim(str(isnull(sum(case [pName] when '语文' then [score] end),0)))
from [scoreInfo] sc where st.[sId]=sc.[rId] )
from [studentInfo] st1 张三
2 李四
3 王五1 数学 87
1 语文 78
2 数学 67
2 语文 97
1 张三 数学87&语文78
2 李四 数学67&语文97
3 王五 数学0&语文0
insert into studentInfo([sId],[sName]) values(1,'张三')
insert into studentInfo([sId],[sName]) values(2,'李四')
insert into studentInfo([sId],[sName]) values(3,'王五')create table scoreInfo(rId int,pName varchar(20),score int)
insert into scoreInfo([rId],[pName],[score]) values(1,'数学',87)
insert into scoreInfo([rId],[pName],[score]) values(1,'语文',78)
insert into scoreInfo([rId],[pName],[score]) values(2,'数学',67)
insert into scoreInfo([rId],[pName],[score]) values(2,'语文',97)select sid,sName,
max(case when pname='数学' then '数学'+ltrim(score) else '数学'+ltrim(0) end)+'&'+
max(case when pname='语文' then '语文'+ltrim(score) else '语文'+ltrim(0) end) as col
from (select * from studentInfo a left join scoreInfo b on a.sid=b.rid)t
group by sid,sName2 李四 数学67&语文97
3 王五 数学0&语文0
1 张三 数学87&语文78
ps:貌似.net版提问的没有sql版的大方,一看排行榜靠前的就属SQL版的多。钻石也多~
string sqlText ="
select 姓名,科目,成绩
from tab1,tab2
where tab1.编号 = tab2.编号";
max(case when pname='数学' then '数学'+ltrim(score) else '数学'+ltrim(0) end)+'&'+
max(case when pname='语文' then '语文'+ltrim(score) else '语文'+ltrim(0) end) as col
from (select * from studentInfo a join scoreInfo b on a.sid=b.rid)t
group by sid,sName1 张三 数学87&语文78
2 李四 数学67&语文97
DECLARE @str VARCHAR(1000)
SET @str='select st.[sId],st.[sName],sc.[newScore] from [studentInfo] st,(select a.[rid],('
SELECT @str=@str+''''+pName+'''+ltrim(str(max(CASE WHEN a.[pName]='''+pName+''' THEN a.[score] ELSE 0 END)))+ ''&''+'
FROM (Select Distinct [pName] From [scoreInfo]) A
set @str=substring(@str,1,len(@str)-6)
SET @str=@str+')as [newScore] from [scoreInfo] a group by a.[rId]) sc where st.[sId]=sc.[rId] '
EXEC(@str)
把这个放在存储过程里
想想加函数不行,不支持动态语句
select @str=isnull(@str+'+''&''+','')+'max(case when pname='''+pname+''' then pname+ltrim(score) end)'
from (Select Distinct [pName] From [scoreInfo]) A
exec('select sid,sName,'+@str+' as col from (select * from studentInfo a join scoreInfo b on a.sid=b.rid)t group by sid,sName')1 张三 数学87&语文78
2 李四 数学67&语文97嘿嘿阿非,不愿写不太好写的了,要不写了老久还不给你分,会比较郁闷的