Reputation: 900
Products
table sample data:
Id | Name | Color | Size |
---|------------|-------------|--------------|
1 | Shirt A | Blue | M
2 | Shirt A | Red | M
3 | Shirt A | Blue | L
4 | Shirt A | Red | L
5 | Shirt B | Black | M
6 | Shirt B | White | M
7 | Shirt B | Black | L
8 | Shirt B | White | L
I would like a SQL query to get the distinct products by color as it follows:
Id | Name | Color |
|------------|-------------|
1/3 | Shirt A | Blue |
2/4 | Shirt A | Red |
5/7 | Shirt B | Black |
5/8 | Shirt B | White |
I hope to have explained my problem well.
Update
Sorry, I forgot to mention that I need the primary keys on the result rows
Upvotes: 0
Views: 347
Reputation: 310
Try this instruction:
SELECT DISTINCT (YOUR_FIELDS)
FROM (YOUR_TABLE);
Set your fields and your table name.
Upvotes: 1
Reputation: 4449
You should be able to use what is called a distinct
clause in your query.
SELECT DISTINCT
Name,
Color
FROM my_table
Here is a link to the w3schools explanation of distinct https://www.w3schools.com/sql/sql_distinct.asp
Upvotes: 1
Reputation: 81930
Pretty much in your title
Select Distinct Name,Color
from YourTable
Returns
Name Color
Shirt A Blue
Shirt A Red
Shirt B Black
Shirt B White
Upvotes: 1