阅读:5529回复:0
mysql 中 DISTINCT不能选择多个字段的解决方法
在实际应用中,我们经常要选择数据库某表中重复数据,通常我们是使用DISTINCT函数。
但DISTINCT只能对一个字段有效,比如: sql="select DISTINCT title from Table where id>0" 当我们需要列出数据中的另一列,比如: sql="select DISTINCT title,posttime from Table where id>0" 得出的结果就不是我们想要的了,所以我们需要用另外的方法来解决这个问题。 下面的是我写的SQL语句,我不知道是不是很好,但愿有更好的人拿出来分享一下: 写法一: sql = "Select DISTINCT(title),posttime From Table1 Where id>0" 写法二: sql = "Select title,posttime From Table1 Where id>0 group by title,posttime" 写法三: sql="select title,posttime from Table where id in (select min(id) from Table |
|
|