0

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

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

don't forget accepting the answer as correct answer so your question is marked as resolved ;-)
1

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;

4 Comments

OP is not clear as OP don't want any repeated 'numbers or names' instead of 'numbers and names'
"How I can I select Name and pid"
using SELECT DISTINCT Name, pid. Anyways as OP has accepted your answer. Now we know that's what he wanted. May be I didn't get it properly.
see my answer, the question is already answered ;-) no problem =)
0

Use GROUP BY

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

Comments

0
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.