使用Distinct返回重复值。如何避免这种情况?

时间:2012-11-26 08:38:59

标签: mysql sql

查询:

  select distinct (Customer) ,CustomerID from history where Customer != '' and        Customer LIKE 'Ram%';

结果:

      Ramer ram100
      Raman ra45
      Raman ra45

2 个答案:

答案 0 :(得分:3)

尝试使用MAXgroup by

 select Customer, MAX(CustomerID) CustomerID
 from   history
 where  Customer != '' AND Customer LIKE 'Ram%'
 GROUP BY Customer 

答案 1 :(得分:0)

如果您还想查看所有客户ID,请使用group_concat

select customer, group_concat(distinct customerid)
from history
where Customer != '' and Customer like 'Ram%'
group by customer