postgresql - Yii2:SQL查询中的函数
问题描述
我需要使用 Yii2 框架从名为 people 的表中选择 21 岁或以下的人。
人=(身份证,生日)
public static function findByBirthday($idper) {
$query = Persons::find()
->select([
Persons::tableName().".[[id]]",
Persons::tableName().".[[birthday]]"
])
->where([
'<=',
'select age(birthday, NOW())',
21
])
return $query->asArray()->all();
}
解决方案
如果生日是有效的 DATE 类型
public static function findByBirthday($idper) {
$query = Persons::find()
->select([
Persons::tableName().".[[id]]",
Persons::tableName().".[[birthday]]"
])
->where([
'<=',
'EXTRACE(year from age(birthday))',
21
])
return $query->asArray()->all();
}
或者
->where( 'EXTRACE(year from age(birthday)) <= 21')
推荐阅读
- javascript - 从无线电输入更改标签的图像
- sql - 记录集中没有结果
- javascript - ajax.reload 后渲染数据表不起作用
- angular - Angular formly templateOptions 描述未显示
- sql-server - 数据库主密钥如何保护证书
- python - 在 __init__ 内使用变量到 __init__ 外
- android - Flutter .DexArchiveMergerException 中的错误
- c# - Azure 存储中 blob 的 Readlines 真的很慢
- python - 获取特定列表范围内的元素
- c++ - 为什么`string str = str;`会编译(并且只是对某些编译器的警告)