sql查询唯一的记录集

时间:2012-05-22 10:01:42

标签: sql

我有一个SQL查询 -

SELECT name_1, dpid_clid, city 
 FROM dts_master_dividend 
 WHERE upper(name_1) like upper('%') 
   AND dpid_clid like upper('IN30290243450560%')
 ORDER BY name_1 asc, dpid_clid asc, dividend_type desc

它将结果显示为 -

ABHINAV  BHARTI IN30290243450560    PUNE
ABHINAV  BHARTI IN30290243450560    DELHI
ABHINAV  BHARTI IN30290243450560    PUNE
ABHINAV  DUBEY  IN30290243450560    PUNE
ABHINAV  BHARTI IN30290243450560    PUNE

但我希望我的结果为 -

ABHINAV  BHARTI IN30290243450560    PUNE
ABHINAV  BHARTI IN30290243450560    DELHI
ABHINAV  DUBEY  IN30290243450560    PUNE

这意味着只有独特的记录集。
我怎么能得到它?

6 个答案:

答案 0 :(得分:2)

尝试:

select distinct name_1, dpid_clid, city
  from dts_master_dividend
 where upper(name_1) like upper('%')
   and dpid_clid like upper('IN30290243450560%')
 order by name_1 asc, dpid_clid asc, dividend_type desc

答案 1 :(得分:2)

试试这个

select name_1, dpid_clid, city 
from dts_master_dividend 
where upper(name_1) like upper('%') and dpid_clid like upper('IN30290243450560%')
group by name_1, dpid_clid ,city

答案 2 :(得分:0)

只需添加DISTINCT关键字:

SELECT DISTINCT name_1, dpid_clid, city 
 FROM dts_master_dividend 
 WHERE upper(name_1) like upper('%') 
   AND dpid_clid like upper('IN30290243450560%')
 ORDER BY name_1 asc, dpid_clid asc, dividend_type desc

这将从结果集中删除重复的行。对于MySQL文档,请参阅here

答案 3 :(得分:0)

使用DISTINCT

SELECT distinct name_1, dpid_clid, city 
 FROM dts_master_dividend 
 WHERE upper(name_1) like upper('%') 
   AND dpid_clid like upper('IN30290243450560%')
 ORDER BY name_1 asc, dpid_clid asc

答案 4 :(得分:0)

它可能取决于您的数据库服务器,但执行此操作的标准方法是使用

SELECT DISTINCT [columns] FROM [table]. 

因此,请尝试在查询中添加DISTINCT

select distinct name_1, dpid_clid, city from dts_master_dividend where upper(name_1) like  upper('%') and dpid_clid like upper('IN30290243450560%') order by name_1 asc, dpid_clid asc, dividend_type desc

有关详细信息,请参阅w3schools page on select distinct

答案 5 :(得分:0)

避免upper(name_1) like upper('%')而不是upper('IN30290243450560%')只使用'IN30290243450560%'

所以你的查询看起来应该像 -

SELECT DISTINT name_1, dpid_clid, city
FROM dts_master_dividend
WHERE dpid_clid like 'IN30290243450560%'
ORDER BY name_1 asc, dpid_clid asc, dividend_type desc;