yii uses postgresql array type query

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.)

Array comparison uses the default B-tree comparison function of the element data type to compare the contents of the array element by element. In a multidimensional array, the elements are accessed in the main order of the row (the last subscript changes the fastest). If the contents of the two arrays are equal but the dimensions are different, the first difference of the dimension information determines the sort order. (This is different from PostgreSQL versions before 8.2: the old version will declare that two arrays with the same content are equal, even if the number of dimensions or subscript ranges are different.)

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}

 --Some comparison operators in array operations
Operator Description
a = b Cube a and b are the same
a && b Cube a and b overlap
a @> b Cube a contains cube b
a <@ b Cube a is contained in cube b Where
[a, b] <[c, d] is less than
[a, b]> [c, d] is greater than

For example, the following example:

       $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 has many rich data types out of the box, from standard numeric data types, to geometric types, and even network data types. Although many people ignore these data types, they are one of my favorite features. The array data type is as you expect, you can store array data in PostgreSQL. With this feature, you can achieve the storage requirements that used to require multiple tables in a single table.

Why use arrays to store data, if you are an application developer, then use the same model in the database to store the data in the program, why not do it. Moreover, this approach can also improve performance.

 

Guess you like

Origin blog.csdn.net/lchmyhua88/article/details/105386115