在数据库中有两个表com_estimation,com_jyoutai
表com_estimation的结构
com_id com_reg_code com_id com_estimation_kbn com_d_no com_ing
2 5 1 10 10 1
2 5 1 10 20 1
2 5 1 10 30 1
2 5 1 10 40 1
2 5 1 20 10 1
2 5 1 20 20 1
2 5 1 20 30 1
2 5 1 20 40 1
数据表中有这样的数据com_estimation_kbn=10表示中间结果,=20表示最终结果。
前五个为主键
表com_jyoutai的结构:
com_id com_reg_code com_id_d com_title_name
2 5 1 数学
2 5 2 语文
前三个字段为主键,和上表一样
现在想得到这样的形式
com_title_name 中间结果 最终结果
请问sql语句应该怎么写啊?
表com_estimation的结构
com_id com_reg_code com_id com_estimation_kbn com_d_no com_ing
2 5 1 10 10 1
2 5 1 10 20 1
2 5 1 10 30 1
2 5 1 10 40 1
2 5 1 20 10 1
2 5 1 20 20 1
2 5 1 20 30 1
2 5 1 20 40 1
数据表中有这样的数据com_estimation_kbn=10表示中间结果,=20表示最终结果。
前五个为主键
表com_jyoutai的结构:
com_id com_reg_code com_id_d com_title_name
2 5 1 数学
2 5 2 语文
前三个字段为主键,和上表一样
现在想得到这样的形式
com_title_name 中间结果 最终结果
请问sql语句应该怎么写啊?
解决方案 »
- 高手帮忙啊
- 路过的遇到过这个问题吗?有关FCKEditor的问题?高难度的,我试了很多次了都没解决?
- cookie 中可不可以保存访问者的浏览器信息
- 绑定控件问题,求代码活例子,可用外加(100)分
- HttpWebRequest的问题
- 怎么在页面中对aspnetdb中的用户进行管理,比如删除和修改,
- C# 开发语音视频聊天
- 新手的问题?
- 碰到个奇怪的事情,我用asp.net做的webpage访问我的webservice,而我的webservice对我的database进行操作,在我机器上一切正常,可别人访
- 页面动态刷新的问题!希望个位高手帮帮小弟!
- 一个关于XML的问题在线等解决就给分(急)
- 哪里能下载asp.net2.0电子书?英文的也可以。
select com_jyoutai.com_title_name ,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code= com_jyoutai.com_reg_code and com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=10) as 中间结果,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code= com_jyoutai.com_reg_code and com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=20) as 最终结果 from com_jyoutai
你试试看:
select com_jyoutai.com_title_name ,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code= com_jyoutai.com_reg_code and com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=10) as 中间结果,(select com_estimation_kbn from com_estimation where com_estimation.com_id =com_jyoutai.com_id and com_estimation.com_reg_code= com_jyoutai.com_reg_code and com_estimation.com_id_d = com_jyoutai.com_id_d and com_estimation_kbn=20) as 最终结果 from com_jyoutai,com_estimation
a.com_title_name,
'中间结果'=case a.com_estimation_kbn when 10 then 10 else 0 end,
'最终结果'=case a.com_estimation_kbn when 20 then 20 else 0 endfrom com_estimation a left join com_jyoutai b
on a.com_id=b.com_id and
a.com_reg_code=b.com_reg_code and
a.com_id_d=b.com_id_d
大概应该是这个样子,修改一下
a.com_title_name,
'中间结果'=sum(case a.com_estimation_kbn when 10 then com_ing else 0 end),
'最终结果'=sum(case a.com_estimation_kbn when 20 then com_ing else 0 end)from com_estimation a left join com_jyoutai b
on a.com_id=b.com_id and
a.com_reg_code=b.com_reg_code and
a.com_id_d=b.com_id_d
group by a.com_title_name