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版本有所不同:旧版本会声明具有相同内容的两个数组是相等的,即使维度或下标范围的数目不同。)

Operator Description Example Result
= equal ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3] t
<> not equal ARRAY[1,2,3] <> ARRAY[1,2,4] t
< less than ARRAY[1,2,3] < ARRAY[1,2,4] t
> greater than ARRAY[1,4,3] > ARRAY[1,2,4] t
<= less than or equal ARRAY[1,2,3] <= ARRAY[1,2,3] t
>= greater than or equal ARRAY[1,4,3] >= ARRAY[1,4,3] t
@> contains ARRAY[1,4,3] @> ARRAY[3,1] t
<@ is contained by ARRAY[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 concatenation ARRAY[1,2,3] || ARRAY[4,5,6] {1,2,3,4,5,6}
|| array-to-array concatenation ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]] { {1,2,3},{4,5,6},{7,8,9}}
|| element-to-array concatenation 3 || ARRAY[4,5,6] {3,4,5,6}
|| array-to-element concatenation ARRAY[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 存储数组数据,有了这个特性,你可以在单个表中实现以往需要多个表才能实现的存储要求。

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

猜你喜欢

转载自blog.csdn.net/lchmyhua88/article/details/105386115
今日推荐