I have a table with a column of type longtext
that stores nutrient values in JSON format. Here's an example of what the data looks like:
[{"label":"N","value":0.1,"percent":10},{"label":"P","value":0.1,"percent":10},{"label":"K","value":0.1,"percent":10},{"label":"S","value":"","percent":""},{"label":"Ca","value":"","percent":""},{"label":"Mg","value":"","percent":""},{"label":"Zn","value":"","percent":""},{"label":"Mn","value":"","percent":""},{"label":"Cu","value":"","percent":""},{"label":"B","value":"","percent":""},{"label":"Al","value":"","percent":""},{"label":"Fe","value":"","percent":""},{"label":"Na","value":"","percent":""},{"label":"Mo","value":"","percent":""}]
Currently I'm pulling all records from the Db and then using a foreach
to iterate through each record and filter out those that don't have N, P, and K with non-zero / empty string percent values.
foreach($record as $rec){
$nutrients = json_decode($rec->nutrients);
$nutrientTextArray = [];
foreach($nutrients as $n){
if(in_array($n->label, ['N', 'P', 'K'])){
if($n->percent != ''){
$nutrientTextArray[] = $n->percent;
} else {
$nutrientTextArray[] = 0;
}
} else {
if($n->percent != ''){
$nutrientTextArray[] = $n->percent . $n->label;
}
}
}
However, iterating through all nutrients for each record takes too long. I tried wildcard matching within the SQL query itself to filter, doing something like the following,
select * from products where nutrients like '%"label":"N"%"label":"P"%"label":"K"%';
but that doesn't take into account the non-zero / empty percent aspect.
How can I modify this query to only select records that have labels N, P, and K, where each of those nutrients all have non-zero / empty string percent values?