我有这样的查询来计算我的location_id
表中的member_location
$stat_location = Location::find()
->select([
'*',
'jum1' => MemberLocation::find()
->select(['COUNT(*)'])
->from('member_location')
->where(['=','a.id','member_location.location_id']),
])
->alias('a')
->asArray()
->all();
,多数民众赞成在查询工作。
在我的Location
表中有一个capacity
列,现在我想根据location_id
表中的金额/计数member_location
来获取是否为完整状态。
我有这样的尝试查询
$stat_location = Location::find()
->select([
'*',
'jum1' => MemberLocation::find()
->select(['COUNT(*)'])
->from('member_location')
->where(['=','a.id','member_location.location_id']),
'status' => "(CASE WHEN jum1 > a.capacity THEN 'full' ELSE 'available' END)",
])
->alias('a')
->asArray()
->all();
但是该查询给我一个错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jum1' in 'field list'
The SQL being executed was: SELECT *, (SELECT COUNT(*) FROM `member_location` WHERE (`a`.`id` = 'member_location.location_id')) AS `jum1`, (CASE WHEN jum1 > a.capacity THEN 'full' ELSE 'available' END) AS `status` FROM `location` `a`
有什么帮助吗?,谢谢
答案 0 :(得分:0)
您不能在WHERE子句中使用SELECT语句中的别名。但是,您可以在HAVING子句中使用它。
或者,将子查询从SELECT语句移到左联接或内部联接。