做个矩阵,其实就是个二维数组,关键是要从数据库里调些数据,sql语句不熟练,就边学边用,同步地记些笔记,方便查阅
关于distinct和order by 发了个帖子(第一次哦)
首帖:
用的是oracle的数据库
目的是想选出唯一的userid(原有重复值),并且按照filepos排序(原filepos是1,2,3.。。不重复的序号)
问题:以下语句都不能满足要求
1)select distinct userid from bbsfile where rootid=371841(不重复,但是没按顺序排)
2)select distinct userid,filepos from bbsfile where rootid=371841 order by filepos(按顺序排列,但是有重复)
3)select distinct userid from bbsfile where rootid=371841 order by filepos(语法出错)
就是想让出现重复值的一列按照没有重复值得一列排序
有一个简单的回复解决了问题
select userid from bbsfile where rootid=371841 group by userid order by min(filepos);
思考的角度变一下就出结果了,这里关键是用到了min 这么简单的就去掉了重复。
分析:distinct 只能选出后面跟的一串字所构成的行,让每一行都不相同。
但是,order by 后面的东西又必须出现在distinct 后面 。所以出现了上述问题。或许还有别的好的解决办法,暂没想到。
考虑时间:由于时间包括日期和时间,如果仅是filetime=xx年月日 返回空,to_char很好用,可以将时间转换为特定格式。
trunc(filetime)='15-10月-07' and to_char(filetime,'hh24') between 18 and 20 group by bbsuserid order by min(filepos)";