Postgresql Yii2:SQL查询中的函数
我需要使用Yii2框架从名为persons的表中选择21岁或21岁以下的人 个人=(身份证、出生日期)Postgresql Yii2:SQL查询中的函数,postgresql,yii2,Postgresql,Yii2,我需要使用Yii2框架从名为persons的表中选择21岁或21岁以下的人 个人=(身份证、出生日期) 公共静态函数findByBirthday($idper){ $query=Persons::find() ->挑选([ Persons::tableName().“[[id]]”, Persons::tableName()。“[[生日]]” ]) ->在哪里([ “如果生日是有效的日期类型 public static function findByBirthday($idper) {
公共静态函数findByBirthday($idper){
$query=Persons::find()
->挑选([
Persons::tableName().“[[id]]”,
Persons::tableName()。“[[生日]]”
])
->在哪里([
“如果生日是有效的日期类型
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();
}
公共静态函数findByBirthday($idper){
$query=Persons::find()
->挑选([
Persons::tableName().“[[id]]”,
Persons::tableName()。“[[生日]]”
])
->在哪里([
“如果生日是有效的日期类型
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();
}
公共静态函数findByBirthday($idper){
$query=Persons::find()
->挑选([
Persons::tableName().“[[id]]”,
Persons::tableName()。“[[生日]]”
])
->在哪里([
“您只需将生日
与21年前的日期进行比较即可-在年龄计算中,应该更好地重用索引而不是表达式,结果将是相同的
public static function findByBirthday($idper) {
$query = Persons::find()
->select([
Persons::tableName() . ".[[id]]",
Persons::tableName() . ".[[birthday]]",
])
->where([
'>=',
'birthday',
new Expression('date :date', [
':date' => date('Y-m-d', strtotime('-21 years')),
]),
]);
return $query->asArray()->all();
}
您只需将生日
与21年前的日期进行比较即可-对于年龄计算,它应该比表达式更好地重用索引,并且结果将是相同的
public static function findByBirthday($idper) {
$query = Persons::find()
->select([
Persons::tableName() . ".[[id]]",
Persons::tableName() . ".[[birthday]]",
])
->where([
'>=',
'birthday',
new Expression('date :date', [
':date' => date('Y-m-d', strtotime('-21 years')),
]),
]);
return $query->asArray()->all();
}
你的代码有什么问题?你有错误?显示准确的错误消息..错误的结果..添加正确的数据样本实际结果和预期结果代码有什么问题..你有错误?显示准确的错误消息..错误的结果..添加正确的数据样本实际结果和预期结果我不认为这会起作用,你可能必须使用new yii\db\Expression('EXTRACE(从年龄(生日)),
而不是'EXTRACE(从年龄(生日)),
我不认为这会起作用,你可能必须使用new yii\db\Expression('EXTRACE(从年龄(生日)),
而不是'EXTRACE(从年龄(生日)算起的年份),