Emily Kenpachi
Emily Kenpachi

Reputation: 57

select statement without including duplicate data

I am trying to select from a database but because the database as duplicate data and the item names in each data may or may not be duplicated. Please look at the example below to understand more

Table shoe

shoeid   pid  Name 
1         1    green
2         1    green
3         2     red
4         3     red

Thats a simple example.

How I can I select Name and pid from this table but i dont want to see any repeated numbers or names. for example i don't want to see red or green or whatever color i have in the database more than once. Please remember I have over 100 colors in the database. The same thing apply to the pid

Upvotes: 0

Views: 156

Answers (4)

user753676
user753676

Reputation:

Use DISTINCT

http://www.mysqltutorial.org/mysql-distinct.aspx
http://www.w3schools.com/sql/sql_distinct.asp

This might be what you want

this gives you only unique results

SELECT DISTINCT Name, pid FROM shoe;

Upvotes: 1

Omesh
Omesh

Reputation: 29091

try this:

SELECT DISTINCT Name FROM table_name;

or if you want maximum id in output:

SELECT Name, MAX(pid) AS id FROM table_name GROUP BY Name;

or if you want comma separated list of ids for that Name:

SELECT Name, GROUP_CONCAT(pid) AS id_list FROM table_name GROUP BY Name;

Upvotes: 1

sudhakar
sudhakar

Reputation: 572

SELECT DISTINCT Name FROM shoe

this query get the unique values , if you want where you can use it by

 SELECT DISTINCT Name FROM shoe WHERE your_key = 'your_key_val'

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

Use GROUP BY

select * from `yourtable` group by `pid`,`Name`

Upvotes: 0

Related Questions