| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 765 人关注过本帖
标题:这样的SQL要怎么写?
只看楼主 加入收藏
sm105096496
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2007-7-11
收藏
 问题点数:0 回复次数:2 
这样的SQL要怎么写?

大哥大姐帮帮我 。。。。
我现在一个表分别是 Users , Integral表
Users表:
Userid ChrName PWD
1 xxx 123456
2 zzz 123456

Integral表:
ID Userid inYear intMonth intGoal intcurrent
1 1 2000 1 2 0
2 1 2000 2 0 0
3 1 2000 3 1 0
. . . . . .
12 1 2000 12 1 0
13 2 2000 1 1 0
14 2 2000 2 2 0
15 2 2000 3 1 0
. . . . . .
24 2 2000 12 3 0

我想得到这样的数据

id ChrName intGoal(按照intMonth 的(1)) intGoal(按照intMonth 的(2))....intGoal(按照intMonth 的(12))
1 xxx 2 0 1
2 zzz 1 1 3

搜索更多相关主题的帖子: SQL Userid Integral Users zzz 
2007-09-25 17:01
风之剑雨飘香
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-10-2
收藏
得分:0 
回复:(sm105096496)这样的SQL要怎么写?
基本就这样,根据需要再修改吧
selecct a.userid,a.chrname,m1.month01,m2.month02,m3.month03,m4.month04,m5.month05, m6.month06,m7.month07,m8.month08,m9.month09,m10.month10,m11.month11,m12.month12
from (select Userid,chrname from Users) a,
(select id,intGoal month01 from Integral where inyear=2002 and intmonth=1) m1,
(select id,intGoal month02 from Integral where inyear=2002 and intmonth=2) m2,
(select id,intGoal month03 from Integral where inyear=2002 and intmonth=3) m3,
(select id,intGoal month04 from Integral where inyear=2002 and intmonth=4) m4,
(select id,intGoal month05 from Integral where inyear=2002 and intmonth=5) m5,
(select id,intGoal month06 from Integral where inyear=2002 and intmonth=6) m6,
(select id,intGoal month07 from Integral where inyear=2002 and intmonth=7) m7,
(select id,intGoal month08 from Integral where inyear=2002 and intmonth=8) m8,
(select id,intGoal month09 from Integral where inyear=2002 and intmonth=9) m9,
(select id,intGoal month10 from Integral where inyear=2002 and intmonth=10) m10,
(select id,intGoal month11 from Integral where inyear=2002 and intmonth=11) m11,
(select id,intGoal month12 from Integral where inyear=2002 and intmonth=12) m12
where a.userid=m1.id and a.userid=m2.id and a.userid=m3.id and a.userid=m4.id and a.userid=m5.id and
a.userid=m6.id and a.userid=m7.id and a.userid=m8.id and a.userid=m9.id and a.userid=m10.id and
a.userid=m11.id and a.userid=m12.id
2007-10-07 10:22
shuzai1985
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-8-22
收藏
得分:0 

select id,ChrName ,case intmonth when 1 then intgoal else ''end as '1月',
case intmonth when 2 then intgoal else ''end as '2月',
case intmonth when 3 then intgoal else ''end as '3月',
case intmonth when 4 then intgoal else ''end as '4月',
case intmonth when 5 then intgoal else ''end as '5月',
case intmonth when 6 then intgoal else ''end as '6月',
case intmonth when 7 then intgoal else ''end as '7月',
case intmonth when 8 then intgoal else ''end as '8月',
case intmonth when 9 then intgoal else ''end as '9月',
case intmonth when 10 then intgoal else ''end as '10月',
case intmonth when 11 then intgoal else ''end as '11月',
case intmonth when 12 then intgoal else ''end as '12月',
from Users , Integral where Users.id = Integral.id  
 order by id


不知道行不行  LZ自己试试

2007-10-07 12:08
快速回复:这样的SQL要怎么写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023099 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved