首页 > 解决方案 > 具有多个值的雄辩范围

问题描述

我正在尝试使用 laravel 范围执行查询。

我有一张带有 id、name 等的“酒店”表,还有一张表,每家酒店都有很多功能,比如:

Featute.id= 1 name=Wifi Hotel_id = 1
Featute.id= 2 name=Wifi in room Hotel_id = 1
Featute.id= 3 name=Wifi in pool Hotel_id = 1
Featute.id= 4 name=Wifi in room Hotel_id = 2
Featute.id= 5 name=Wifi in pool Hotel_id = 2

我想获得所有带有“房间内无线网络”和“游泳池内无线网络”的酒店

我需要在范围内使用哪种方法?查询会是什么样子?

我已经绑定了这个,但它返回了具有这些功能的酒店。

select `hotels`.`name` as `name`, 
       `hotels`.`delegation_id` as `delegation`, 
       `hotels`.`jpcode` as `jpcode`, 
       `hotels`.`iata` as `iata`, 
       `hotels`.`checked` as `checked`, 
       `hotels`.`gen_note` as `gen_note`, 
       `hotels`.`des_note` as `des_note`, 
       `hotels`.`feat_note` as `feat_note`, 
       `hotels`.`img_note` as `img_note`
from `hotels`
    inner join `features` on `hotels`.`id` = `features`.`hotel_id` 
where `features`.`name` in('Wifi in room','Wifi') 
group by hotels.id

标签: phpmysqllaraveleloquent

解决方案


每个功能需要一个 JOIN:

select `hotels`.`name` as `name`, 
       `hotels`.`delegation_id` as `delegation`, 
       `hotels`.`jpcode` as `jpcode`, 
       `hotels`.`iata` as `iata`, 
       `hotels`.`checked` as `checked`, 
       `hotels`.`gen_note` as `gen_note`, 
       `hotels`.`des_note` as `des_note`, 
       `hotels`.`feat_note` as `feat_note`, 
       `hotels`.`img_note` as `img_note`
from `hotels`
    inner join `features` as `f1` on `hotels`.`id` = `f1`.`hotel_id`
    inner join `features` as `f2` on `hotels`.`id` = `f2`.`hotel_id`
where `f1`.`name` = 'Wifi in room'
    and `f2`.`name` = 'Wifi'
group by hotels.id

推荐阅读