dimanche 31 mars 2019

How to make Eloquent query for Group by, with HAVING and MAX

I have SQL query which I need to convert to Eloquent and no idea where to write MAX conditions. How it is posible to convert it without much Raw sql? Thank you!

SELECT bike_id
FROM bike_filters
GROUP BY bike_id
HAVING
   MAX(bike_category_id in (416,11111)) = 1
   AND MAX(bike_category_id in (5555,779)) = 1
   AND MAX(bike_category_id in (5555,772)) = 1

Table Structure:

| id | bike_id | bike_category_id |
| 1  | 3       | 416              |
| 2  | 3       | 779              |
| 3  | 3       | 344              |
| 4  | 3       | 332              |
| 5  | 4       | 444              |
| 5  | 5       | 555              |

The purpose of this query is to get bike_ids, which has all parameters by the query - bike can have 20 filters, but if user searches by 5 and bike matches them, we get bike_id by this query.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2V8bdjs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire