A
A
Alex Pro2014-02-06 09:59:49
MySQL
Alex Pro, 2014-02-06 09:59:49

Intersection of MySQL Set Groups

Please help:
There is a table of goods:
| id | caption |
| 1 | first |
| 2 | second |
| 3 | third |
There is also a table of parameters combined into sets
. Sets:
| id | set_name |
| 1 | set1 |
| 2 | set2 |
| 3 | set3 |
Options:
| id | set_id | params |
| 1 | 1 | 2014 |
| 2 | 1 | 2013 |
| 3 | 2 | param1 |
| 4 | 2 | param2 |
| 5 | 2 | param3 |
| 6 | 3 | param1_3 |
Well, actually the table of links:
| id_item | id_param |
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
| 2 | 4 |
| 2 | 5 |
| 3 | 2 |
| 3 | 3 |
| 3 | 6 |
What is:
At the input there is a multidimensional array, with nested parameters sorted by sets:

$forSelect = array(
 'set1' => array( '1' , '2' ),
 'set2' => array( '3' , '5' ),
);

What you need:
Make a selection of products that contain at least one for each set of input parameters, i.e. if there is a parameter '1' (2014) from 'year' in the product, then it will only be included in the selection if the product has a relationship with '3', or '5' from the second set of input data
P.S.: Maybe I should rebuild as - a base for greater convenience, if such a task will cause difficulties?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2014-02-06
@Stillborn

As a variant of the solution "in the forehead":

SELECT *
FROM items
WHERE id in (SELECT id_item
       FROM items_param
       WHERE id_param in (1, 2))
  AND id in (SELECT id_item
       FROM items_param
       WHERE id_param in (3, 5))

And so on conditionally for each set of input data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question