Tag Archives: sql

MYSQL: how to get one row from multiple rows with same column id value

i have two tables: product and productVariation: product variation table contains multiple entries for the same product but with different attributes.

example: product: id name 1 top 2 bottom

id name
1 top
2 bottom

productVariation table:

id product_id color_id size_id
1 1 4 7
2 1 5 7
3 1 8 7
4 1 7 10

so for the above tables, when I am querying that data for the products list to display, let’s say the user wants data for size_id 7 then we have 3 possibilities (3 different colors) but on the products list page, I just want to show one entry for this product. My question is how can I just get one entry based on the product_id from the product variation table. I already wrote a query that is filtering the data based on color and size but I get multiple entries for the same product, I just want one of these rows.

Thank you! 🙂