Go3Team
Go3Team

Reputation: 145

How to select unique values?

I am trying to get this query to only display 1 result (not LIMIT) in this example:

SELECT company_id  FROM `location` WHERE `state` = 'XX'

It results are similar to this:

16 9 9 9 9 30 30 30 45 50 50 50 50 57 7 79 80 80 80 80 120 120 120 120 120 128 131 120 265 265

I am aiming for these results:

16 9 30 45 50 57 7 79 80 120 128 131 120 265

Upvotes: 0

Views: 94

Answers (3)

Nirali Joshi
Nirali Joshi

Reputation: 2008

Use DISTINCT keyword

SELECT DISTINCT company_id  FROM `location` WHERE `state` = 'XX'

It will give the desired result.

Upvotes: 1

Akash KC
Akash KC

Reputation: 16310

Simply use DISTINCT when you want to retrieve distinct result set.....

SELECT DISTINCT company_id  FROM `location` WHERE `state` = 'XX'

You can use Group By as an alternative to DISTINCT in following way:

 SELECT company_id  FROM `location` WHERE `state` = 'XX' GROUP BY company_id  

The DISTINCT and GROUP BY generates same execution plan as performance wise though I strongly recommend you to use DISTINCT in your scenario......

Upvotes: 0

Himanshu
Himanshu

Reputation: 32602

Just add DISTINCT keyword before you column name

SELECT DISTINCT company_id FROM `location` WHERE `state` = 'XX'

The DISTINCT keyword in a SELECT statement removes duplicates returned by a query.

Upvotes: 3

Related Questions