一道数据库的面试题求解

今天面试碰到一到面试题不会做吗,请高手帮忙我看看,谢谢了
题目1:如何查找和删除数据库中重复数据
题目2:有如下表,用一句SQL查询出每个用户最近登录的记录(每个用户只显示一条最近登录记录)
id name email lastLogon
100 test4 [email protected] 2007-11-25 16:31:26
130 test1 [email protected] 2008-10-05 17:21:16
101 test1 [email protected] 2007-09-19 18:30:32
120 test2 [email protected] 2008-08-08 10:21:23
150 test2 [email protected] 2007-02-21 14:23:12

1--就拿你问题2的数据举例,邮箱和name同时重复,只保留其中一条
delete from 表名 where id not in (select min(id) from 表名 group by name,email)

2--
select t1.* from 表名 t1,
(select name,max(lastlogon) lastlogon from 表名 group by name) t2
where t1.name=t2.name and t1.lastlogon=t2.lastlogon
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-29
题目1:新建一个与原表结构相同的表,然后对原表group by 所有字段 插入新表,删除原表,导入新表数据到原表。
题目2:select name,email,max(lastlogon) from tablename group by name,email