简体   繁体   English

Yii WHERE IN 有多个 where

[英]Yii WHERE IN with multiple where

I have a table that looks like this:我有一张看起来像这样的表:

id
listingId
type
distance

Where listingId is a foreign key to a table of listings, type is a handle for type of place, and distance is just an integer.其中,listingId 是列表的外键,type 是地点类型的句柄,距离只是一个整数。 And I want to be able to get all listingIds that fulfil a set of 1-5 queries.我希望能够获得满足一组 1-5 个查询的所有列表 ID。 This is what I have so far:这是我到目前为止:

$records = MyActiveRecord::find()
            ->select('listingId')
            ->where(['and', 'type="hospital"', "distance <= 130"])
            ->orWhere(['and', 'type="airport"', 'distance <= 600'])
            ->all();

Now this will return all hits as expected, but what I'm after are just the listingIds that show up twice (in this scenario, if there are three WHERE clauses I would want the ones that have 3 hits, and so on).现在这将按预期返回所有命中,但我所追求的只是出现两次的 listingIds(在这种情况下,如果有三个WHERE子句,我会想要那些有 3 个命中的子句,依此类推)。

I'm guessing I would have to go for a WHERE IN , but I'm not sure how to do that with a subquery?我猜我必须去WHERE IN ,但我不确定如何使用子查询来做到这一点?

Any suggestions?有什么建议? Thanks.谢谢。

EDIT (specifying a bit)编辑(指定一点)

So what I'm after is any listingIds that have both hospital and airport within the given distances.所以我所追求的是在给定距离内同时具有医院机场的任何列表 ID。 The query given above gives me any listingId where it's either of them.上面给出的查询为我提供了任何列表 ID,其中任何一个。 And in that case wherever there are two identical listingId it would mean that I have a match on both, and that's the listingId I want.在这种情况下,只要有两个相同的listingId ,就意味着我在两者上都有匹配项,这listingId我想要的listingId

I ended up getting the way to do it with SQL, so in the end I just used findBySql and wrote out my query:我最终得到了使用 SQL 的方法,所以最后我只使用了findBySql并写出了我的查询:

$distances = [ // will come from query
    [
        'type' => 'hospital',
        'distance' => 130
    ],
    [
        'type' => 'airport',
        'distance' => 600
    ]
];

$query = 'select listingId from myTable
      where type = "' . $distances[0]['type'] . '"
      and distance <= ' . $distances[0]['distance'];

foreach(array_slice($distances, 1) as $dist) {
    $query = 'select listingId from myTable
        where type = "' . $dist['type'] . '"
        and distance <= ' . $dist['distance'] . '
        and listingId in (' . $query . ')';
}

Which ends up looking something like this最终看起来像这样

select listingId from myTable
where type = 'bar'
and distance <= 130
and listingId in (
  select listingId from myTable
  where type = 'airport'
  and distance <= 600
)
$subQuery = MyActiveRecord::find()->select('listingId')->where(['and',['type' => 'airport'],['<=', 'distance', 600]]);
$records = MyActiveRecord::find()
                        ->select('listingId')
                        ->where(['and', ['type' => 'hospital'], ['<=', 'distance', 130]])
                        ->orWhere(['in', 'listingId', $subQuery])
                        ->all();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM