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
,part2
andpart3
, 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.
2
Answers
Possible solution.
fiddle
If you don’t want to convert your criteria array to subquery text (which must be concatenated into) then you may convert it to JSON array form, provide it to MySQL and parse to separate values in static subquery using JSON_TABLE().
An alternative approach, building on @CBroe’s suggestion. If you can sort the criteria before you insert them into the SQL statement, then this might work for you.
First normalise the parts list, then reassemble a string of parts in sorted order, separated and wrapped by commas:
Finally wrap in a overall query that applies the criteria and extracts the matching rows:
(reduce the number of ? parameters as appropriate)
Again, this assumes you’re able to sort the criteria parameters before binding them to the statement.