如何使用distinct关键字删除重复值

时间:2013-08-24 06:58:05

标签: mysql

我有一张包含这样记录的表

*************************** 1. row *************************

   did: 98

 brand_name: Aarther P (100+500)

generic: Paracetamol, Aceclofenac

tradename: Aarther P (100+500)

manfactured: Rekvina Pharmaceuticals

unit: 500mg/100mg

type: Tablet

quantity: 10Tablet

 price: 27.9

*************************** 2. row *************************

did: 99

brand_name: Aarther-P

generic: Aceclofenac, Paracetamol

tradename: Aarther-P

manfactured: Rekvina Pharmaceuticals

unit: 100mg/500mg

type: Tablet

quantity: 10Tablet

price: 27.9

*************************** 3. row ************************

did: 100

brand_name: Aarticef (1000mg)

generic: NULL

tradename: Aarticef (1000mg)

manfactured: Alpic Remedies Ltd

unit: 1000mg/vial

type: Injection

quantity: 1Vial

price: 96

我想消除重复的制造名称,因为我已经执行了以下查询

select brand_name,generic,manfactured from drugs_info group by
manfactured;

但它只显示第1和第3条记录,但我想要第2条记录

*************************** 1. row *************************

did: 98

 brand_name: Aarther P (100+500)

generic: Paracetamol, Aceclofenac

tradename: Aarther P (100+500)

manfactured: Rekvina Pharmaceuticals

unit: 500mg/100mg

type: Tablet

quantity: 10Tablet

 price: 27.9

*************************** 3. row ************************

 did: 100

 brand_name: Aarticef (1000mg)

generic: NULL

tradename: Aarticef (1000mg)

manfactured: Alpic Remedies Ltd

unit: 1000mg/vial

type: Injection

quantity: 1Vial

price: 96

如何根据我的要求编写选择查询。

2 个答案:

答案 0 :(得分:0)

SELECT * FROM
(select brand_name,generic,manfactured from drugs_info ORDER BY did DESC) abc
 group by manfactured;

答案 1 :(得分:0)

要删除mysql中的重复项,请使用mysql的多表删除语法:

delete d2
from mytable d1
join mytable d2 on d2.manfactured = d1.manfactured
    and d2.did > d1.did

d2.did > d1.did的额外连接条件确保两个行都不会连接到自身,并且每个副本只有一半被删除,而且还保留了最低的id行。