今天一个sql简单面试题,拿来跟大家分享下
java吧
全部回复
仅看楼主
level 3
qq2511296 楼主
题目:有个xiaofei表里面的字段如果。
要求用一条sql语句查询出所有user的最后一次的消费记录
2011年11月02日 11点11分 1
level 3
qq2511296 楼主
呃 看来沉的好快~1
2011年11月02日 11点11分 2
level 11
如果主键是 按时间循序添加的话
select max 后 group by userid
再与本表相连?
几年没开发了,脑子都不好用了
2011年11月02日 13点11分 3
level 11
确实应该这样
2011年11月02日 14点11分 6
level 3
qq2511296 楼主
mysql:先让它执行order by的语句
SELECT userid,money,xiaofeidate FROM (SELECT * FROM xiaofei ORDER BY xiaofeidate DESC) c
GROUP BY userid ORDER BY xiaofeidate DESC
Oracle:
select userid,money,xiaofeidate
from (select userid,money,xiaofeidate,row_number()over
(partition by userid order by xiaofeidate desc) c
from xiaofei) a where a.c=1 order by xiaofeidate desc
mysql&Oracle: 通过同时匹配2个字段来查询
select * from xiaofei a where exists( select 1 from (
select userid,max(xiaofeidate) xiaofeidate from xiaofei
group by userid) b where a.userid=b.userid and a.xiaofeidate=b.xiaofeidate)
2011年11月03日 02点11分 7
level 4
select * from xiaofei where xiaofeidate in (SELECT max(xiaofeidate) FROM xiaofei group by userid)
2011年11月03日 06点11分 9
level 11
目测5楼的方法最好
9楼的跟我3楼说的类似
2011年11月03日 11点11分 10
level 1
菜鸟问a,not exists()哪来的,什么意思,谢谢
2011年11月03日 11点11分 11
level 3
qq2511296 楼主
in 和 not in 意思差不多
你可以百度下这个的用法和区别
2011年11月03日 15点11分 12
level 11
效率比 in 之类的高
2011年11月03日 18点11分 13
level 1
不就是要查出第一条记录么?
select xf.* from xiaofei xf where to_char(xf.xiaofeidate)=(select to_char(max(x.xiaofeidate)) from xiaofei x)
2011年11月04日 03点11分 14
level 6
最厚一条记录??用@@identity吧,详情请按F1
2011年11月04日 04点11分 15
level 3
qq2511296 楼主
你自己想下
select to_char(max(x.xiaofeidate)) from xiaofei x) 你这里是获得所有记录的最大的日期,而是每个用户的最大日期~! 看懂题目再做
2011年11月04日 05点11分 16
level 1
select main(里面是要求出的数据) from 表名
2011年11月04日 14点11分 17
level 9
挺简单的:
Select userid, money, xiaofeidate
From xiaofei x
Where xiaofeidate in (Select MAX(xiaofeidate) From xiaofei
Where userid = x.userid)
2011年11月04日 16点11分 19
1