doupi4649 2019-01-11 13:00
浏览 11

样本不包括表中的值

I have objects connected mano through the intermediate table.

class City{ /**
     * @return \yii\db\ActiveQuery
     */
    public function getReviews()
    {
        return $this->hasMany(Reviews::className(), ['id' => 'review_id'])
            ->viaTable('city_review', ['city_id' => 'id']);
    }
}

And get review for city:

  $reviews = $city2->getReviews()->all();

Question: How select reviews from reviews table, which are not in the table city_review?

  • 写回答

1条回答 默认 最新

  • dtqjbbr5283 2019-01-11 13:12
    关注

    I'm not familiar with yii activerecord syntax

    But the query you need is below.

    SELECT review.* FROM review 
    LEFT JOIN city_review cr ON cr.city_id = review.id
    WHERE cr.id IS NULL
    

    This code should result similar to what you need but you need to test.

    $reviews = Reviews::find()
        ->select('review.*')
        ->leftJoin('city_review', '`city_review`.`city_id` = `review`.`id`')
        ->where(['city_review.id' => NULL])
        ->with('city_reviews')
        ->all();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式