yii 使用postgresql 数组类型查询

Array comparisons compare the array contents element-by-element, using the default B-tree comparison function for the element data type. In multidimensional arrays the elements are visited in row-major order (last subscript varies most rapidly). If the contents of two arrays are equal but the dimensionality is different, the first difference in the dimensionality information determines the sort order. (This is a change from versions of PostgreSQL prior to 8.2: older versions would claim that two arrays with the same contents were equal, even if the number of dimensions or subscript ranges were different.)

数组比较使用元素数据类型的默认B树比较函数逐元素比较数组内容。在多维数组中,元素按行的主要顺序访问(最后一个下标变化最快)。如果两个数组的内容相等但维数不同,则维数信息的第一个差异决定排序顺序。(这与8.2之前的PostgreSQL版本有所不同:旧版本会声明具有相同内容的两个数组是相等的,即使维度或下标范围的数目不同。)

OperatorDescriptionExampleResult
=equalARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]t
<>not equalARRAY[1,2,3] <> ARRAY[1,2,4]t
<less thanARRAY[1,2,3] < ARRAY[1,2,4]t
>greater thanARRAY[1,4,3] > ARRAY[1,2,4]t
<=less than or equalARRAY[1,2,3] <= ARRAY[1,2,3]t
>=greater than or equalARRAY[1,4,3] >= ARRAY[1,4,3]t
@>containsARRAY[1,4,3] @> ARRAY[3,1]t
<@is contained byARRAY[2,7] <@ ARRAY[1,7,4,2,6]t
&&overlap (have elements in common)ARRAY[1,4,3] && ARRAY[2,1]t
||array-to-array concatenationARRAY[1,2,3] || ARRAY[4,5,6]{1,2,3,4,5,6}
||array-to-array concatenationARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]{{1,2,3},{4,5,6},{7,8,9}}
||element-to-array concatenation3 || ARRAY[4,5,6]{3,4,5,6}
||array-to-element concatenationARRAY[4,5,6] || 7{4,5,6,7}

 --数组运算中的一些比较符
运算符                  描述
a = b                   立方体a和b是相同的
a && b                  立方体a和b重叠
a @> b                  立方体a包含立方体b
a <@ b                  立方体a被包含在立方体b中
[a, b] < [c, d]         小于
[a, b] > [c, d]         大于

例如下面的例子:

       $sensitive = new SensitiveInfo();
        $c = new CDbCriteria();
        $cd_company_id = 11;
        $customer_id = 1;
        $c->addCondition('share_company_id || cd_company_id @> array['.$cd_company_id.']');
        $c->compare('level', 1);
        $c->addInCondition('customer_id', array(0,$customer_id));
        $c->with = array(
            'sensitiveCondition' => array(
                'alias' => 's',
                'joinType' => 'left join',
                'select'  => '*',
            )
        );
        $sensitiveRows = $sensitive->findAll($c);

PostgreSQL 有很多丰富的开箱即用的数据类型,从标准的数字数据类型、到几何类型,甚至网络数据类型等等。虽然很多人会忽略这些数据类型,但却是我最喜欢的特性之一。而数组数据类型正如你所期望的,可以在 PostgreSQL 存储数组数据,有了这个特性,你可以在单个表中实现以往需要多个表才能实现的存储要求。

为什么要使用数组来存储数据,如果你是应用开发人员,那么在数据库中使用同样的模型来存储程序中的数据,何乐而不为呢。况且这样的做法还能提升性能。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值