I need to know if the following can be resolved with a SQL query and how. I have done it with PHP in different ways but the memory skyrockets and the server takes a long time to return the results. 🙁
- I have a table called
'parts'with many rows (but many). - Each of the rows has the columns
part1,part2andpart3, all with different values.
Example:
+---------+-------+-------+
| part1 | part2 | part3 | <-- Column Name
+---------+-------+-------+
| chamber | wheel | door | <-- Value
+---------+-------+-------+
On the other hand I have a list of three values that come from an external source:
$list = ['mirror', 'seat', 'door'];
I need to select the rows whose columns contain all those values, regardless of the order. An example of a valid row would be:
+-------+--------+-------+
| part1 | part2 | part3 |
+-------+--------+-------+
| door | mirror | seat |
+-------+--------+-------+
Sometimes the external list doesn’t have 3 values, just 1 or 2:
$list = ['antenna'];
… Then the valid rows would be those that contain that value, for example:
+--------+--------+---------+
| part1 | part2 | part3 |
+--------+--------+---------+
| engine | mirror | antenna |
+--------+--------+---------+
- Additional info: In each row, the columns do not have repeated values, so this feature can be ignored in the query.
- I need to know how to make a query for SELECT and another for COUNT.
- It is necessary that the result be resolved through a MySQL query, without using PHP for filtering.