joinwith - Subconsulta Yii2 en registro activo
yii2 find join (3)
Prueba esta consulta
$models = BaseTwitter::find()->where(''id NOT IN (SELECT base_id from base_followers)'')->all();
¿Cómo puedo convertir este sql en consulta de registro activa?
SELECT * FROM `base_twitter` WHERE id NOT IN (SELECT base_id from base_followers)
Suponiendo que sus modelos se denominen
BaseTwitter
y
BaseFollower
consecuencia, esto debería funcionar:
$subQuery = BaseFollower::find()->select(''id'');
$query = BaseTwitter::find()->where([''not in'', ''id'', $subQuery]);
$models = $query->all();
// 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]);