0

I have the following MySQL table products:

id, code, name, color
1, 'D555', 'Men T-shirt', ''
2, 'D556', 'Black Men T-shirt', 'D556'
3, 'D557', 'Silver Men T-shirt', 'D556'
4, 'D558', 'Men T-shirt', ''
5, 'D559', 'Black Men T-shirt', 'D559'
6, 'D560', 'White Men T-shirt', 'D559'

The column color is not empty, if the product have another colors, for example: The product with code 'D556' is black and it has silver color, too.

I'm using SELECT * FROM products, but now I want to SELECT the main products (without the sub-colors), i.e: D555, D556, D558, D559.

  • And after that I'll SELECT the sub-colors under the product (since it's about e-store for clothes).

Is it possible with that kind of structure of the table?

Thank you very much for the attention!

2 Answers 2

1
SELECT * FROM products WHERE color = '' or code = color;

You could consider normalizing your table in two tables like the following:

products(id, code, name)
alt_products(id, id_products, color)
Sign up to request clarification or add additional context in comments.

1 Comment

Hihihi, very clever!! I'll use the first option, because I upload ~300 products every month and it's very hard to optimize for me. Thanks alot for the help!!!
0

May be useful be this:

SELECT `id`, `code`, `name`, GROUP_CONCAT(`color` SEPARATOR ', ')  as `colors`
FROM products 
GROUP BY `code`

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.