2015-09-24 311 views
0

嘿,我想從表中得到幾個datarows。但不想讓這些公司翻倍。所以我想用DISTINCT條款來減少公司。是這樣的可能嗎?WHERE公司=(DISTINCT公司)

SELECT Company, Firstname, Surname, eMail, Adress, Place, Phone, Fax FROM `contact_list` 
WHERE Company = (DISTINCT Company as `compName`) 

FROM:

Company | Firstname | Surname | eMail    | Adress  | Place  | Phone | Fax 
DELL | Max  | Master | [email protected] | dellstreet | chicago | 543478 | 543267 
DELL | John  | Doe  | [email protected] | dellstreet | chicago | 543462 | 543267 
SHELL | Charlie | Fame | [email protected] | nicestreet | shelltown | 5546546 | 54535 

期待:

Company | Firstname | Surname | eMail    | Adress  | Place  | Phone | Fax 
DELL | Max  | Master | [email protected] | dellstreet | chicago | 543478 | 543267 
SHELL | Charlie | Fame | [email protected] | nicestreet | shelltown | 5546546 | 54535 
+0

請提供一些示例數據和預期輸出。 – Wanderer

+0

然後更改標題.. – Wanderer

+0

第1列和第10列包含什麼? –

回答

1

不能使用distinct這種方式。而是使用group by

SELECT column1, Company, - column10 FROM `contact_list` 
GROUP BY company 
0

您可以使用這樣的...

select distinct a, c from table_c 

或者

select a, b from table_c group by a, b 

組由會更有益的,如果你想使用類似計數一些聚合函數或總和...

select a, b, count(*) from table_c group by a, b 

    select a, b, sum(d) from table_c group by a, b 

a,b表示你的列名....

我希望這是工作....

0

想必,你必須在表中的唯一ID還是創建日期或識別的一些方法,其有重複時需要的行。如果是這樣的話:

select cl.* 
from contact_list cl join 
    (select cl.company, max(id) as maxid 
     from contact_list 
     group by cl.company 
    ) ccl 
    on cl.company = ccl.company and cl.id = ccl.maxid; 

這將返回最大id的行。