mysql - Yii2 匹配 IN 子句中的所有值

标签 mysql yii2

我正在尝试获取与 IN 子句中的所有值相匹配的查询。这就是我现在在 UsersSearch.php 模型中所拥有的:

    $categoryIdsMatching = UsersCategoriesAssn::find()
        ->select('userID')
        ->distinct(true)
        ->joinWith('userCategory')
            ->andWhere(['IN', 'usersCategories.id', $this->catNameSearch])
        ->column();
    $query->andWhere(['userID'=>$categoryIdsMatching]);

但它获取的记录至少匹配其中一个值...我如何设置 andWhere 子句来匹配所有值而不是其中一些值?

最佳答案

这可能类似于:

$categoryIdsMatching = UsersCategoriesAssn::find()
    ->select('userID')
    ->distinct(true)
    ->joinWith('userCategory')
        ->andWhere(['IN', 'usersCategories.id', $this->catNameSearch])
        ->groupBy('userTable.userID')
        ->having('COUNT(userTable.userID) ='.count($this->catNameSearch));

可能没有其他方法可以过滤 IN 运算符来匹配 MySQL 中的所有值。

关于mysql - Yii2 匹配 IN 子句中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797949/

相关文章:

mysql - 如何从 CloudBees Jenkins 访问 MySQL?

model - Yii2 - 从更新表单更新另一个模型的单个字段

css - Yii2 Gridview - 水平顶部滚动条

yii2 从关系表中选择列问题

php - Yii2 Gridview - 如何在页脚属性上使用总计

activerecord - Yii2事件记录模型不保存数据

mysql - 使用两个单独的 SQL 语句还是一个 SQL 语句与 JOIN 结合使用更快?

php - 在不重新加载页面的情况下刷新 Mysql 计数

mysql - InnoDB 在外键上使用 where 子句时锁定不同的索引

mysql - SQL 修改字母顺序,包括复合字符