satya
satya

Reputation: 3560

SQL query for ignore duplicate data using MySQL

I need one help.I need to fetch some data from one table but one column value should not duplicate.I am explaining my query below.

select  Product_name,pro_Id from db_product_info order by Product_name

Here I need the duplicate value of product name should not fetch.Please help me.

Upvotes: 0

Views: 65

Answers (3)

slaakso
slaakso

Reputation: 9080

The basic way of removing duplicates is to use DISTINCT:

select distinct Product_name,pro_Id 
from db_product_info 
order by Product_name

If you data has Product_name's with different pro_Id's, you need to decide which pro_Id to choose from. In this case you need to use aggregate function (min, max) combibed with GROUP BY:

select Product_name, max(pro_Id)
from db_product_info 
group by Product_name
order by Product_name

Upvotes: 0

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

Try this:

 select  Product_name,pro_Id from db_product_info group by product_name order by Product_name

Upvotes: 0

Vahid Hallaji
Vahid Hallaji

Reputation: 7467

For this case, You can use group by

SELECT  
    Product_name,
    pro_Id 
FROM 
    db_product_info 
GROUP BY Product_name 
ORDER BY Product_name ASC

See SqlFiddle

Upvotes: 1

Related Questions