公司=(DISTINCT公司)

时间:2015-09-24 10:39:58

标签: mysql 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  | m.master@dell.com | dellstreet | chicago   | 543478  | 543267 
DELL    | John      | Doe     | j.doe@dell.com    | dellstreet | chicago   | 543462  | 543267 
SHELL   | Charlie   | Fame    | charlie@shell.com | nicestreet | shelltown | 5546546 | 54535

期待:

Company | Firstname | Surname | eMail             | Adress     | Place     | Phone   | Fax
DELL    | Max       | Master  | m.master@dell.com | dellstreet | chicago   | 543478  | 543267 
SHELL   | Charlie   | Fame    | charlie@shell.com | nicestreet | shelltown | 5546546 | 54535

3 个答案:

答案 0 :(得分:1)

您不能以这种方式使用distinct。而是使用group by

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

答案 1 :(得分:0)

你可以这样使用......

select distinct a, c from table_c

或者

select a, b from table_c group by a, b
如果你想使用像count或sum这样的聚合函数,

group by会更有帮助。

  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代表您的专栏名称....

我希望这有效......

答案 2 :(得分: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的行。

相关问题