E
E
eugene1592021-09-13 00:54:28
MySQL
eugene159, 2021-09-13 00:54:28

How to select records from the table if they do not exist (you need to select products for which some property is not filled)?

There are three tables:
options
product_options
products

The options table has fields:
id, key, name

The product_options table has fields:
product_id, key, value

The products table has fields:
id, name, image, url

I need to select all products that have the manufacturer property is not filled.
The problem is that if it is not filled, then there is simply no record in the table.
And if there is no entry, how can I select products?

I tried to be smart with LEFT JOIN, but it didn't work. Nothing is returned.
Here is my code:

SELECT po.value as value, o.name as name
FROM  options o
LEFT JOIN product_options po ON (po.key = o.key)
LEFT JOIN products p ON (p.id = po.product_id)
WHERE po.value IS NULL
AND po.key = 'manufacturer'

Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-09-13
@Rsa97

SELECT *
FROM  `products`
WHERE `id` NOT IN (
  SELECT `product_id`
    FROM `product_options`
    WHERE `key` = 'manufacturer'
  )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question