WHERE in(子查询)子句 yii
WHERE in (subquery) clause yii
我有一个 mysql
这样的查询
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName
FROM
`notification`
INNER JOIN
`event`
ON
event.id = notification.source_id
INNER JOIN
`user` as v
ON
v.id = notification.user_id
AND
notification.activity_type = "checkin"
where
user_id in
(SELECT friend.friend_id from friend WHERE friend.user_id=1 AND friend.is_active=1))
UNION
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName
FROM
`notification`
INNER JOIN
`user` as u
ON
u.id = notification.source_id
INNER JOIN
`user` as v ON v.id = notification.user_id
AND
notification.activity_type = "friend"
where user_id in
(SELECT friend.friend_id from friend WHERE friend.user_id=1 AND friend.is_active=1) )
我想在 yii2
中编写该查询,但我不知道如何在子句中的 where
中编写子查询。
到目前为止我已经做到了
$query2 ->select(['notification.id', 'notification.user_id AS user_id', 'notification.activity_type', 'notification.source_id', 'concat_ws(" ",u.firstname,u.lastname) as sourceName','concat_ws(" ",v.firstname,v.lastname) as ActorName','user_image.imagepath as image'])
->from('notification' )
->innerJoin('user as u', 'u.id = notification.source_id')
->innerJoin('user_image','user_image.user_id = notification.user_id')
->innerJoin('user as v', 'v.id = notification.user_id AND notification.activity_type = "friend"')
->where('user_image.imagetype="profile"')
->andWhere(['user_id'=>('SELECT friend.friend_id from friend WHERE friend.user_id='.$id.' AND friend.is_active=1')]);
$query ->select(['notification.id','notification.user_id AS user_id','notification.activity_type', 'notification.source_id', 'event.title as sourceName','concat_ws(" ",v.firstname,v.lastname) as ActorName','organiser.image as image'])
->from('notification')
->innerJoin('event', 'event.id = notification.source_id')
->innerJoin('organiser','organiser.organiser_id = event.organiser_id')
->innerJoin('user as v', 'v.id = notification.user_id AND notification.activity_type = "checkin"')
->Where(['in', 'user_id', [488,489]])
->union($query2);
生成这样的命令查询
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` AS `sourceName`, concat_ws(" ",v.firstname,v.lastname) as ActorName, `organiser`.`image` AS `image`
FROM
`notification`
INNER JOIN
`event`
ON
event.id = notification.source_id
INNER JOIN
`organiser`
ON
organiser.organiser_id = event.organiser_id
INNER JOIN
`user` `v`
ON
v.id = notification.user_id
AND
notification.activity_type = "checkin"
WHERE
`user_id` IN (:qp0, :qp1))
UNION
( SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName, concat_ws(" ",v.firstname,v.lastname) as ActorName, `user_image`.`imagepath` AS `image`
FROM
`notification`
INNER JOIN
`user` `u`
ON
u.id = notification.source_id
INNER JOIN
`user_image`
ON
user_image.user_id = notification.user_id
INNER JOIN
`user` `v`
ON
v.id = notification.user_id
AND
notification.activity_type = "friend"
WHERE
(user_image.imagetype="profile") AND (`user_id`=:qp2) )
但是它不起作用,那么正确的语法是什么
如果 where
in 可以写成 inner join
也欢迎提出建议
这将使编写查询变得非常容易
谢谢
你可以使用 Yii2 Query Builder,这里是一个例子。
$subQuery = (new \yii\db\Query())->select([
'users.id as userId',
'users.first_name',
'users.username',
'users.last_name',
'users.sector_id',
])
->from(['users'])
->where(['=', 'role_id', 3])
->orWhere(['=', 'role_id', 2])
->groupBy('users.id');
$query = (new \yii\db\Query())->select(['user.*', 'user_address.*'])
->from(['user'=>$subQuery])
->leftJoin('user_address', 'user.userId = user_address.user_id')
->where(['between', 'latitude', $min_lat, $max_lat])
->andWhere(['between', 'longitude', $min_lon, $max_lon])
->groupBy('user.userId');
Try This
$query = (new Query())
->select('notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName')
->from('notification')
->innerJoin('event' ,'event.id = notification.source_id')
->innerJoin('user v' ,'v.id = notification.user_id')
->where([
'user_id' => (new Query())
->select('friend.friend_id')
->from('friend')
->where([
'friend.user_id' => 1,
'friend.is_active' => 1
])
])->andWhere([
'notification.activity_type' => "checkin"
]);
$query2 = (new Query())
->select('`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName')
->from('notification')
->innerJoin('user v' ,'v.id = notification.user_id')
->where([
'user_id' => (new Query())
->select('friend.friend_id')
->from('friend')
->where([
'friend.user_id' => 1,
'friend.is_active' => 1
])
])->andWhere([
'notification.activity_type' => "checkin"
])->union($query)->all();
我有一个 mysql
这样的查询
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName
FROM
`notification`
INNER JOIN
`event`
ON
event.id = notification.source_id
INNER JOIN
`user` as v
ON
v.id = notification.user_id
AND
notification.activity_type = "checkin"
where
user_id in
(SELECT friend.friend_id from friend WHERE friend.user_id=1 AND friend.is_active=1))
UNION
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName
FROM
`notification`
INNER JOIN
`user` as u
ON
u.id = notification.source_id
INNER JOIN
`user` as v ON v.id = notification.user_id
AND
notification.activity_type = "friend"
where user_id in
(SELECT friend.friend_id from friend WHERE friend.user_id=1 AND friend.is_active=1) )
我想在 yii2
中编写该查询,但我不知道如何在子句中的 where
中编写子查询。
到目前为止我已经做到了
$query2 ->select(['notification.id', 'notification.user_id AS user_id', 'notification.activity_type', 'notification.source_id', 'concat_ws(" ",u.firstname,u.lastname) as sourceName','concat_ws(" ",v.firstname,v.lastname) as ActorName','user_image.imagepath as image'])
->from('notification' )
->innerJoin('user as u', 'u.id = notification.source_id')
->innerJoin('user_image','user_image.user_id = notification.user_id')
->innerJoin('user as v', 'v.id = notification.user_id AND notification.activity_type = "friend"')
->where('user_image.imagetype="profile"')
->andWhere(['user_id'=>('SELECT friend.friend_id from friend WHERE friend.user_id='.$id.' AND friend.is_active=1')]);
$query ->select(['notification.id','notification.user_id AS user_id','notification.activity_type', 'notification.source_id', 'event.title as sourceName','concat_ws(" ",v.firstname,v.lastname) as ActorName','organiser.image as image'])
->from('notification')
->innerJoin('event', 'event.id = notification.source_id')
->innerJoin('organiser','organiser.organiser_id = event.organiser_id')
->innerJoin('user as v', 'v.id = notification.user_id AND notification.activity_type = "checkin"')
->Where(['in', 'user_id', [488,489]])
->union($query2);
生成这样的命令查询
(SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` AS `sourceName`, concat_ws(" ",v.firstname,v.lastname) as ActorName, `organiser`.`image` AS `image`
FROM
`notification`
INNER JOIN
`event`
ON
event.id = notification.source_id
INNER JOIN
`organiser`
ON
organiser.organiser_id = event.organiser_id
INNER JOIN
`user` `v`
ON
v.id = notification.user_id
AND
notification.activity_type = "checkin"
WHERE
`user_id` IN (:qp0, :qp1))
UNION
( SELECT
`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName, concat_ws(" ",v.firstname,v.lastname) as ActorName, `user_image`.`imagepath` AS `image`
FROM
`notification`
INNER JOIN
`user` `u`
ON
u.id = notification.source_id
INNER JOIN
`user_image`
ON
user_image.user_id = notification.user_id
INNER JOIN
`user` `v`
ON
v.id = notification.user_id
AND
notification.activity_type = "friend"
WHERE
(user_image.imagetype="profile") AND (`user_id`=:qp2) )
但是它不起作用,那么正确的语法是什么
如果 where
in 可以写成 inner join
这将使编写查询变得非常容易
谢谢
你可以使用 Yii2 Query Builder,这里是一个例子。
$subQuery = (new \yii\db\Query())->select([
'users.id as userId',
'users.first_name',
'users.username',
'users.last_name',
'users.sector_id',
])
->from(['users'])
->where(['=', 'role_id', 3])
->orWhere(['=', 'role_id', 2])
->groupBy('users.id');
$query = (new \yii\db\Query())->select(['user.*', 'user_address.*'])
->from(['user'=>$subQuery])
->leftJoin('user_address', 'user.userId = user_address.user_id')
->where(['between', 'latitude', $min_lat, $max_lat])
->andWhere(['between', 'longitude', $min_lon, $max_lon])
->groupBy('user.userId');
Try This
$query = (new Query())
->select('notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, `event`.`title` as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName')
->from('notification')
->innerJoin('event' ,'event.id = notification.source_id')
->innerJoin('user v' ,'v.id = notification.user_id')
->where([
'user_id' => (new Query())
->select('friend.friend_id')
->from('friend')
->where([
'friend.user_id' => 1,
'friend.is_active' => 1
])
])->andWhere([
'notification.activity_type' => "checkin"
]);
$query2 = (new Query())
->select('`notification`.`id`, `notification`.`user_id` AS `user_id`, `notification`.`activity_type`, `notification`.`source_id`, concat_ws(" ",u.firstname,u.lastname) as sourceName,concat_ws(" ",v.firstname,v.lastname) as ActorName')
->from('notification')
->innerJoin('user v' ,'v.id = notification.user_id')
->where([
'user_id' => (new Query())
->select('friend.friend_id')
->from('friend')
->where([
'friend.user_id' => 1,
'friend.is_active' => 1
])
])->andWhere([
'notification.activity_type' => "checkin"
])->union($query)->all();