web-dev-qa-db-fra.com

Sous-requête Yii2 dans l'enregistrement actif

Comment puis-je convertir ce sql en requête d'enregistrement active

SELECT * FROM `base_Twitter` WHERE id NOT IN (SELECT base_id from base_followers)
21
Ngoun Thavy

En supposant que vos modèles soient nommés BaseTwitter et BaseFollower en conséquence, cela devrait fonctionner:

$subQuery = BaseFollower::find()->select('id');
$query = BaseTwitter::find()->where(['not in', 'id', $subQuery]);
$models = $query->all();
55
arogachev
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = (new \yii\db\Query)->from('user')->where(['active' => true])
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);

// subquery can also be a string with plain SQL wrapped in parenthesis
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = "(SELECT * FROM `user` WHERE `active` = 1)";
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);
8
Oleg

Essayez cette requête

$models = BaseTwitter::find()->where('id NOT IN (SELECT base_id from base_followers)')->all();
1
Yasin Patel