读书人

使用distinct只能提出一条列的值吗?解

发布时间: 2012-03-19 22:03:04 作者: rapoo

使用distinct只能提出一条列的值吗?
比如数据库中哟字段值为:

username pwd pic
user1 1234 1.gif
user2 www 2.gif
user2 nnnn 3.gif
user3 ddd 4.gif


select distinct username from tables
可以提出 user1
user2
user3

如果我要这样写:select distinct username,pic from tables
提出来的是
user1 1.gif
user2 2.gif
user2 3.gif
user3 4.gif


但如果我想要
user1 1.gif
user2 2.gif
user3 4.gif
distinct该如何写?



[解决办法]
distinct 是去除重复行,因为
user2 2.gif
user2 3.gif
不是重复行,所以都能取出来
[解决办法]
楼上已经回答完了,不知道你要那么读取出来有什么用途,可以把你实际需要达到的目的说详细点,大家帮你给个思路。
[解决办法]
distinct 没有办法

可以

select username,pic from tables where pic <> '3.gif '

或者

select username,pic from tables
where pic not in(select max(pic) from tables group by username having count(username)> 1)

如果需要结果为:
user1 1.gif
user2 3.gif
user3 4.gif

修改上面的max为min

读书人网 >ASP

热点推荐