I have a normalized database, where a superior table contains different products and a subordinated table characteristics. The subordinate table is linked to the superior table by a foreign key. This means that there may be several occurrences of one and the same characteristic_id (= foreign key) in the subordinate table if several characteristics match the properties of a product, e.g.
Superior table (product_main_table):
product_id product_name
23 apple
24 orange
25 strawberry
Subordinate table (product_characteristics):
characteristic_id product_name characteristic
23 apple green
23 apple sweet
23 apple small
23 apple american
24 orange orange
24 orange sourly
24 orange big
24 orange african
25 strawberry red
25 strawberry sweet
I have an html-form to read out the data and there should also be a possibility to search for all products that DO NOT match certain characteristics. However, this does not work. When I enter the following request:
SELECT DISTINCT main.product_name
FROM product_main_table main, product_characteristics prodchar
WHERE prodchar.characteristic != 'sweet'
the result is 'apple', 'orange' and 'strawberry', wheresas it should be 'orange', only. The other two products are selected of course, because they have other characteristics than 'sweet' that are selected.
I would be more than happy, if the solution would fit into my general SQL-request
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE $masterarray";
where $selection, $tabelle
and $masterarray
get their contents from the html-entries.
How do I have to make my where-statement, so that a product is not selected, if it contains a single characteristic that should not be present?