I have table like this
id | item_id | item_key | item_value
------------------------------------
1 | 5 | rating | 6.4
2 | 5 | country | US
3 | 6 | rating | 7.4
4 | 6 | country | UK
5 | 7 | rating | 6.5
6 | 7 | country | US
7 | 8 | rating | 7.9
8 | 8 | country | UK
9 | 5 | genre | Action
10| 5 | year | 2019
11| 6 | genre | Drama
12| 7 | lang | EN
I want select item_id where have country = US and rating not empty and order by rating
EDITED : and ability to check another item_key's values like "genre = 'drama' and year = '2019'" etc
i have more than 10 item_key's variables not only rating and country but if know the method i'll apply for the others
I'm make like this and it's work but my problem with order by rating
$sql = "SELECT metas.* FROM metas ";
if ($cat)
$sql .= "LEFT JOIN articles ON metas.item_id = articles.id ";
$sql .= "GROUP BY metas.item_id HAVING (sum(metas.meta_key = 'rating' AND metas.meta_value != '' AND metas.item_type = '1') AND ";
foreach ($filters as $filter_key => $filter_val){
if (!empty($filter_val)){
if (array_key_exists($filter_val, ${'m_'.$filter_key.'s'})){
if ($filter_key == "genre")
$sql .= (($counter == 1) ? "" : "AND ")."sum(".($cat ? "articles.category = '".$cat."' AND " : "") ."metas.meta_key = '".$filter_key."' AND metas.meta_value LIKE '%".$filter_val."%') ";
else
$sql .= (($counter == 1) ? "" : "AND ")."sum(".($cat ? "articles.category = '".$cat."' AND " : "") ."metas.meta_key = '".$filter_key."' AND metas.meta_value = '".$filter_val."') ";
$trusted_filters[$filter_key] = $filter_val;
$counter++;
} else {
$errs++;
}
}
}
$sql .= ") ORDER BY FIELD(metas.meta_key, 'rating') DESC, metas.meta_value DESC";
Like I say, it's work but order by not working, I need sort it by item_value based on item_key with rating value.