具有内部联接的复杂jQuery查询构建器

时间:2019-10-28 16:44:54

标签: jquery mysql

我有一个这样的表VEHICLES

-------------------------------------------------------
| ID  |   Brand   |   License_plate      |  Model      |
-------------------------------------------------------
| 1   |   IVECO   |   XA123WE            |  GRANDX    |
-------------------------------------------------------
| 2   |   IVECO   |   CF556XD            |  TOURS     |
-------------------------------------------------------
| 3   |   FIAT    |   AS332ZZ            |  Punto     |
-------------------------------------------------------

对于每辆车,我有一个或多个INSURANCE(日期较大的是我需要根据今天的日期检查保险是否仍然有效)

-------------------------------------
| ID  |   vehicle_ID   |   Expire   |
-------------------------------------
| 1   |   1            | 2018-10-19 |
-------------------------------------
| 2   |   1            | 2019-10-20 |
-------------------------------------
| 3   |   2            | 2019-11-25 |
-------------------------------------
| 4   |   2            | 2018-10-20 |
-------------------------------------

在上面的示例中,假设今天是2019年10月28日,ID为1的车辆的保险已过期,ID为2的车辆的保险仍然有效(2019-11-25),ID 3的车辆尚无保险

现在我想将默认搜索框迁移到jquery查询生成器。

查询在vehicles表中进行查询时没问题。

$('#queryBuilder').queryBuilder({
      plugins: ['bt-tooltip-errors'],
      filters: [{
        id: 'vehicles.Brand',
        label: 'Targa',
        type: 'string',
        operators: [ 'equal', 'not_equal', 'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', 'is_empty', 'is_not_empty' ]
      },
      {
        id: 'vehicles.License_plate',
        label: 'Marca',
        type: 'string',
        operators: [ 'equal', 'not_equal', 'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', 'is_empty', 'is_not_empty' ]
      },
      {
        id: 'vehicles.Model',
        label: 'Modello',
        type: 'string',
        operators: [ 'equal', 'not_equal', 'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', 'is_empty', 'is_not_empty' ]
      }]
    });

我的表单将查询和参数发送到我的PHP页面,并且该函数返回我的车辆的所有ID:

function get_ids_vehicles_by_query($query = "", $array_params = array()) {
    global $db;

    if ( $query != "" && count($array_params) ) {

        try {
            $rs = $db->prepare("SELECT DISTINCT vehicles.ID FROM vehiclesWHERE " . $query);
            $rs->execute($array_params);
            $ris = $rs->fetchAll(PDO::FETCH_ASSOC);
            return $ris;

        } catch(PDOException $e) {
            die($e);
        }

    }

    return array();

}

但是现在我需要在我的jquery查询构建器中添加一个选择,该选择根据我的选择来搜索每个车辆的最高保险:

 {
    id: 'insurances.custom_field_i_dont_know',
    label: 'Insurance',
    type: 'integer',
    input: 'select',
    values: {
      0: 'Insurance not present',
      1: 'Insurance still active',
      2: 'Insurance expired'
    },
    operators: ['equal', 'not_equal']
  }

我不确定如何修改我的get_ids_vehicles_by_query以添加一个INNER JOIN(我认为)以仅过滤针对每个车辆ID分组的最高日期保险,并根据我的custom_field_i_dont_know值进行过滤:

0:我只需要过滤未注册保险的车辆(我的例子:车辆ID 3)

1:我只需要过滤仍处于激活状态的车辆(我的例子:车辆ID 2)

2:我只需要过滤保险到期的车辆(我的例子:车辆ID 1)

此外,仍然需要保留第一个jquery查询构建器的Brand,License_plate和Model的过滤器。

JQUERY QUERY BUILDER的工作方式如下:http://jsfiddle.net/fr0z3nfyr/vwyLq21m/2/

有人建议如何编辑我的get_ids_vehicles_by_query函数吗?

0 个答案:

没有答案
相关问题