MySQL:避免选择具有相同用户标识的列

时间:2012-05-29 03:20:52

标签: php mysql

尝试选择所有列但避免使用多个相同的用户ID。

$result = mysql_query("select *, DISTINCT userid from ringtones where deletedbyuser='0' and (lang='$lang' or lang='en') order by id desc limit 10");

请帮忙。

表格结构:

id  int(10)
userid  int(10)
artist  varchar(100)
title   varchar(100)
lang    varchar(2)
public  int(1)
status  int(1)
deletedbyuser   int(1)

1 个答案:

答案 0 :(得分:3)

你可以

select r.* from ringtones r 
    inner join (select MIN(id) as id, userid from ringtones 
                      where deletedbyuser='0' and (lang='$lang' or lang='en') 
                      group by userid) s
    on r.id = s.id
order by r.id desc
limit 10

如果有多个,则会选择添加的第一个。