通过PHP使用postgresql的Age函数

时间:2018-05-24 15:29:22

标签: php postgresql

我试图通过年龄范围过滤我的结果。在我的数据库中,我有一个包含列' dob'的表,因此我尝试使用postgreSql的AGE函数来获取#do; dob'的年龄。但它不起作用。以下是我的代码。

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);
    }
}

当我尝试将上述sql写入Postgre时,我收到的错误是Age不是函数定义的。我希望年龄用户范围内的所有用户都在过滤栏中选择。感谢帮助。 TIA

1 个答案:

答案 0 :(得分:1)

尝试使用<html> <body> <h4>A nested List:</h4> <ul style="PADDING-LEFT: 12px"> <li>Coffee</li> <li>Tea <ul> <li>Black tea</li> <li>Green tea</li> </ul> </li> <li>Milk</li> </ul> </body> </html> 代替now()。此外,如果您只想比较年份,请使用timestamp,这将只给您一年。 假设你的dob是日期或时间戳类型:

EXTRACT YEAR form AGE()

如果&#39; dob&#39;是一些varchar或文本类型,像这样 - $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