SQL of Oracle Database --- select 2
Previously, I talked about some simple use of the select statement. This time, I will learn more about the select statement.
Including: where condition limit query, range query, fuzzy query, and sorting of query results.
Where condition limit Query [SQL] select * from emp where emp. deptno = 10; -- Query all employees whose apartment number is 10
Select * from emp where job = 'cler'; -- note that cler' is case sensitive.
We know that Condition is composed of column name expressions, constants, and comparison operators. What comparison operators are there?
Common comparison Operators
The comparison operators above will not be tried one by one! In the future.
Range Query (Between and)
In reality, we need to query a data segment so that we can use between and to implement [SQL]-query employees with salaries-
Select * from emp where sal between 4000 and 8000; Tips: note that after between, you must first write a low value before writing a high value. Otherwise, the statement will not be wrong.
But it is impossible to query the results!
Fuzzy search (Like, Not Like)
Like Fuzzy queries are generally used for character matching. The wildcard characters "%" and "_" can be used for character matching:
%: Represents any character, including zero; _: represents any character;
For a simple example: [SQL] select * from emp where ename like '_ M %'; -- the employee who queries the second character of the name is "M". However, here we need to consider: what if the query string contains "%" or?
Solution: Use escape
Simple Example: [SQL] insert into emp (Ename, Empno) VALUES ('K % iritor ', 100); -- insert a statement manually
Select * from emp where ename like 'K/% iritor 'escape '/'; add the "/" escape Character to escape (you can specify the escape character as needed ), escape Character
Escape to the original character. The Escape Character is specified to be related to the value of the field to be queried.
As for not like, there will be no demonstration too much!
Result sorting
ASC: sort in ascending order. The default value is. [SQL] select * from emp order by sal; DESC: sort in descending order [SQL] <span style = "color: #000000;"> select * from emp order by sal; how can I sort multiple columns in a single column? [SQL] <pre name = "code" class = "SQL"> -- first, sort the salary in ascending order. If the salary is the same, sort the name in descending order.
</Pre> select * from emp order by sal asc, ename desc; <br>
<Pre> </pre>
<Pre> </pre>
That's all. Some comparison operators in the above table will be used in the future!