GaussDB SQL basic syntax example-array expression

1. Introduction SQL is a standard computer language used to access and process databases. GaussDB supports the SQL standard (main features of SQL2, SQL3 and SQL4 are supported by default).

This series will be introduced based on the online document "Cloud Database GaussDB—SQL Reference".

Welcome to GaussDB database array expressions. In this article, we will introduce the concept, syntax and usage of array expressions in GaussDB database. GaussDB is a high-performance, highly scalable distributed database that is widely used in various business scenarios. Array expression is a powerful feature in GaussDB database, which allows users to use array operators and functions to process array type data in database queries. By studying this article, you will understand how to use array expressions in the GaussDB database to simplify the data processing process and improve query efficiency, thereby better meeting business needs.

2. The concept of conditional expressions and common array expressions in GaussDB GaussDB supports SQL language and some extended functions. In GaussDB, array expressions are used to process array type data. Here are some common array expressions:

1、IN :expression IN (value [, ...])

Use the IN operator to determine whether an array is contained in a given list of values. Returns true if the array contains all values ​​specified in parentheses; false otherwise.

2、NOT IN :expression NOT IN (value [, ...])

Use the NOT IN operator to determine whether an array is not contained in a given list of values. Returns true if the array does not contain any of the values ​​specified in parentheses; false otherwise.

3、ANY(array):expression operator ANY (array expression)

The ANY operator is used to determine whether an array satisfies any condition in a given array. Among them, operator is a comparison operator, such as =, <, >, etc. If the array on the left meets any condition in the array on the right, returns true; otherwise, returns false.

4、SOME (array) :expression operator SOME (array expression)

The SOME operator is used to determine whether an array meets at least one condition in a given array. Among them, operator is a comparison operator, such as =, <, >, etc. Returns true if the array on the left meets at least one condition in the array on the right; otherwise, returns false. It should be noted that the usage of SOME in GaussDB is the same as ANY, so the above example also applies to SOME.

5、ALL (array) :expression operator ALL (array expression)

ALL operator is used to determine whether an array satisfies all conditions in a given array. Among them, operator is a comparison operator, such as =, <, >, etc. Returns true if the array on the left meets all conditions in the array on the right; otherwise, returns false.

3. Commonly used array expressions in GaussDB (syntax + examples) 1. expression IN (value [, ...]) The bracket on the right is an array expression list. The result of the expression on the left is compared with the contents of the expression list. The result of IN is true if the contents of the list match the result of the expression on the left. If there is no matching result, the result of IN is false.

When using in, null data will be ignored and null data will not be queried. If the expression result is null, or the expression list does not meet the conditions of the expression and the right-hand expression list returns at least one empty result, the return result of IN is null instead of false. This processing method is consistent with the Boolean combination rules of SQL returning null values.

Example: Basic table: teacher table, curriculum table

--Whether substitute teachers can be matched in the teacher table

SELECT * FROM course WHERE teid IN (SELECT teid FROM teacher );

2. expression NOT IN (value [, ...]) The right bracket is an expression list. The result of the expression on the left is compared with the contents of the expression list. If there is no content in the list that matches the result of the expression on the left, the result of NOT IN is true. If there is matching content, the result of NOT IN is false.

If the query statement returns an empty result, or the expression list does not meet the conditions of the expression and the right expression list returns at least one empty result, the return result of NOT IN is null, not false. This processing method is consistent with the Boolean combination rules of SQL returning null values.

Example (see screenshot above for basic tables course and teacher):

--Whether substitute teachers can be matched in the teacher table

SELECT * FROM course WHERE teid NOT IN (SELECT teid FROM teacher );

3. Expression operator ANY/ SOME (array expression) The bracket on the right is an array expression, which must produce an array value. The result of the expression on the left uses operators to calculate and compare the results of each row of the array expression, and the comparison result must be a Boolean value.

The result of ANY is true if the comparison results in at least one true value. If the comparison result has no true value, the result of ANY is false. If the result has no truth value and the array expression produces at least one value that is null, then ANY evaluates to NULL, not false. This processing method is consistent with the Boolean combination rules of SQL returning null values. Example: Basic table: Department 1, Department 2

--Find employees in department 1 who are older than employees in department 2

SELECT * FROM department1

WHERE age > ANY (SELECT age FROM department2);

SOME is similar to ANY:

--Find employees in department 1 who are older than employees in department 2

SELECT * FROM department1

WHERE age > SOME (SELECT age FROM department2);

Additional instructions:

any is greater than a certain value in the subquery result < any is less than a certain value in the subquery result = any is greater than or equal to a certain value in the subquery result <= any is less than or equal to a certain value in the subquery result = any Equal to a certain value in the subquery result, equivalent to IN != any Not equal to a certain value in the subquery result 4. expression operator ALL (array expression) The bracket on the right is an array expression, which must produce an array value. The result of the expression on the left uses operators to calculate and compare the results of each row of the array expression, and the comparison result must be a Boolean value.

The result of ALL is true if all comparison results are true (including the case where the array does not contain any elements). The result of ALL is false if one or more comparisons result in false. If the array expression produces a NULL array, the result of ALL is NULL. If the left-hand expression evaluates to NULL, the result of ALL is usually NULL (some loose comparison operators may give different results). In addition, if the array expression on the right contains null elements and the comparison result does not have a false value, the result of ALL will be NULL (some non-strict comparison operators may get different results), not TRUE. This processing method is consistent with the Boolean combination rules of SQL returning null values. Example: (See the screenshot above for the basic tables department1 and department2)

SELECT * FROM department1

WHERE age > ALL (SELECT age FROM department2);

Additional instructions:

all greater than all values ​​in the subquery result < all less than all values ​​in the subquery result = all greater than or equal to all values ​​in the subquery result <= all less than or equal to all values ​​in the subquery result = all equal to the subquery result All values ​​in != all are not equal to any value in the subquery result, which is equivalent to NOT IN IV. Summary Through the introduction of this article, I believe you have already had an in-depth understanding of the array expressions of the GaussDB database. Array expression is a very practical function in the GaussDB database. It can help you process array type data more efficiently, simplify query statements, and improve query efficiency. Hope this article is helpful to you.

IntelliJ IDEA 2023.3 & JetBrains Family Bucket annual major version update new concept "defensive programming": make yourself a stable job GitHub.com runs more than 1,200 MySQL hosts, how to seamlessly upgrade to 8.0? Stephen Chow's Web3 team will launch an independent App next month. Will Firefox be eliminated? Visual Studio Code 1.85 released, floating window Yu Chengdong: Huawei will launch disruptive products next year and rewrite the history of the industry. The US CISA recommends abandoning C/C++ to eliminate memory security vulnerabilities. TIOBE December: C# is expected to become the programming language of the year. A paper written by Lei Jun 30 years ago : "Principle and Design of Computer Virus Determination Expert System"
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/gaussdb/blog/10321093