douxiong5972 2018-05-24 15:29
浏览 33
已采纳

通过PHP使用postgresql的Age函数

I am trying to filter my results through Age range. In my db, I have a table which has a column 'dob', so I am trying to use AGE function of postgreSql to get the age from 'dob'. But it's not working. Following is my code.

contact.php

<div class="form-group">
    <label> Filter by Age</label>
    <div class="input-group">
        <input id="start-age" type="text" name="from-age" class="form-control" placeholder="from" />
        <input id="end-age" type="text" name="to-age" class="form-control" placeholder="to" />
    </div>
</div>

<script>
    $(document).ready(function() {
        $('body').on('change', '#end-age', function () {
            var from_age = $('#start-age').val();
            var to_age = $(this).val();

            var queryString1;
            var queryString2;
            if(location.search && from_age != -1 && to_age != -1){
                queryString1 = getQueryObj(location.search);
                queryString2 = getQueryObj(location.search);
                $.each(queryString1,function(i,e){
                    queryString1[i] = decodeURIComponent(e);
                });
                $.each(queryString2,function(i,e){
                    queryString2[i] = decodeURIComponent(e);
                });
                queryString1.from_filter = from_age;
                queryString2.to_filter = to_age;

            } else if(from_age == -1 && to_age == -1){
                queryString1 = getQueryObj(location.search);
                queryString2 = getQueryObj(location.search);
                delete queryString1.from_filter;
                delete queryString2.to_filter;
            } else {
                queryString1 = {from_filter:from_age};
                queryString2 = {to_filter:to_age};
            }
            window.location.replace('/user?'+ $.param(queryString1) + '&' + $.param(queryString2));
        });
    });

</script>

contactController.php

<?php
class userController extends AdminController {

    function __construct(){
        parent::__construct("Contact","contacts");
    }

    function index(Array $params = []){

        $ageSql = "";
        if(isset($_GET['from_filter']) && isset($_GET['to_filter'])) {
            $from_age = $_GET['from_filter'];
            $to_age = $_GET['to_filter'];
            $ageSql = "SELECT id FROM user WHERE AGE(timestamp dob) >= $from_age AND AGE(timestamp dob) <= ($to_age)";
            $params['queryOptions']['where'][] = "contact_id = $ageSql";

        }
        parent::index($params);
    }
}

When I tried the above sql into Postgre, I am getting an error that Age is not a function defined. I want all the users in the range of the age user selects in the filter bar. Help is appreciated. TIA

  • 写回答

1条回答 默认 最新

  • douhan8009 2018-05-24 18:45
    关注

    Try to use now() instead of timestamp. Also if you want to compare only years use EXTRACT YEAR form AGE(), which will give you only year. Assuming your dob is of date or timestamp type:

    $ageSql = "SELECT id FROM table WHERE EXTRACT(YEAR FROM AGE(now(), dob)) >= 25 AND EXTRACT(YEAR FROM AGE(now(), dob)) <= 40";
    $params['queryOptions']['where'][] = "contact_id IN $ageSql"; //use 'IN' in case more than 1 row
    

    And if 'dob' is of some varchar or text type, cast it like this- dob::timestamp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因