Home Database Mysql Tutorial Oracle学习笔记之高级查询

Oracle学习笔记之高级查询

Jun 07, 2016 pm 03:10 PM

为列起别名 目的 我们进行查询时 经常查询某一列时使用的是表达式 SELECT enamme,sal*12 FROM emp 这样不好的地方是第二列在查询

为列起别名

目的 我们进行查询时 经常查询某一列时使用的是表达式 SELECT enamme,sal*12 FROM emp

这样不好的地方是第二列在查询后列用使用的就是sal*12.这样的可读性比较差为此我们会为列起别名,来增加可读性

别名本身不区分大小写,若希望区分,则别名需要使用双引号,当别名中含有空格,也应使用双引号

SELECT ename,sal*12 "Annual Salary" FROM emp;

WHERE子句

用于在查询数据的过程中过滤记录的,只有满足WHERE子句中的条件的记录才会被查询出来, 数据库在查询表的时候,每一条记录都要经过一次WHERE的过滤

查看工资小于2000的

SELECT ename,sal FROM emp WHERE sal

查看部门不属于10的员工信息

SELECT ename,sal,job FROM emp WHERE deptno !=10;

查询1980年出生的

SELECT ename,sal,hiredate FROM emp WHERE hiredate>TO_DATE('1980','YYYY');

查询sal大于1000 且工作为clerk

SELECT ename,sal,job FROM emp WHERE sal>1000 AND job='CLERK';

查询工资大于1500的CLERK 或者是不限制工资的所有SALESMAN

SELECT * FROM emp WHERE sal>1500 AND job='CLERK' OR job ='SALESMAN';

提高优先级的意思是 查看工资大于1500的CLERK 和SALESMAN

SELECT * FROM emp WHERE sal>1500 AND (job='CLERK' OR job ='SALESMAN');

查看emp表中ename 第二个字母的是A的名字

SELECT * FROM emp WHERE ename LIKE '_A%';

IN 比较符 可以比较等于列别中的其中之一

SELECT * FROM emp WHERE job IN('MANAGER','CLERK'); ==SELECT * FROM emp WHERE job='MANAGER' OR job='CLERK';

查询不等于MANAGER 和CKERK的所有工作

SELECT * FROM emp WHERE job NOT IN('MANAGER','CLERK');

SELECT * FROM emp WHERE sal BETWEEN 1500 AND 3000;

SELECT * FROM emp WHERE sal>ANY(3000,2000,4000);

SELECT * FROM emp WHERE sal*12>50000;

SELECT ename,sal FROM emp WHERE UPPER(ename) = upper('rose');

DISTINCT关键字用于去除给定列的重复数据 查看公司都有哪些职位,重复的去除

SELECT DISTINCT job FROM emp;

当我们对多个列进行去重复时,表示这几个列的值的组合没有重复的

SELECT DISTINCT deptno,job FROM emp;

ORDER BY子句,用于对结果集进行排序

ORDER BY子句只能出现在SELECT语句的最后。

ORDER BY 后面可以指定若干字段,排序优先级为从左到右,

ASC 表示升序,默认就是升序,所以可以不写

DESC表示降序

按照部门编号从小刀大进行排序结果集

SELECT * FROM emp ORDER BY deptno

查询emp表里部门为10的工资和姓名 并降序排列

SELECT ename,sal FROM emp WHERE deptno =10 ORDER BY  sal DESC;

在排序一个含有NULL值的字段时,NULL值被认为是无限大,所以在降序排序时,NULL值会出现在最前面

-------------------------------------------------------------------------------------------

聚合函数

MAX和MIN

SELECT MAX(sal) FROM emp;

***聚合函数都是忽略NULL值

SELECT MAX(sal) as "最大",MIN(sal) as "最小" FROM emp;

SELECT AVG(NVL(comm,0) ) as "平均值" FROM emp;

SELECT AVG(sal),SUM(sal) FROM emp;

SELECT COUNT(comm) FROM emp;--统计条数,不统计值

SELECT COUNT(*) FROM emp;--统计整张表有多少条记录

GROUP BY子句 用于将表里的数据进行 分组,分组原则为GROUP BY 后面给定的字段的值相同的记录看做一组

查看每个部门的最大工资 最小工资等:

SELECT MAX(sal),MIN(sal),AVG(sal),SUM(sal),deptno FROM emp GROUP BY deptno;

聚合函数又称组函数

SQL有一个要求

在SELECT子句红若出现了组函数,那么不在组函数中的其他字段,必须出现GROUP BY 子句中,但是反过来则不是必须的

若字段中每条疾苦的值都不重复,那么该字段通常不应该在GROUP BY子句中作为分组的参照。

若指定了GROUP BY子句,那么SELECT子句中不在组函数中的其他字段且不在GROUP BY子句中则不能出现

WHERE 与HAVING的区别

相同点,都是用作过滤,只不过HAVING是用在第二次过滤的,WHERE是用在第一次查询表的时候过滤条件的。

HAVING是在第一次查询后,得到结果的基础上再次进行过滤使用的。

---------------------------------------

查询优先级

from子句,执行顺序,从后往前,从右到左 数据量少的放后面

where子句 执行顺序为自上而下,从右到左,将能过滤掉最大数量记录的条件卸载where子句的最右

group by 执行顺序从左往右分组

select子句,少用*号 尽量取字段名

----------------------------------------------------------------------------

关联查询

N张表联合查询,,至少要有N-1个连接条件,否则会产生笛卡尔积:A表的记录总数*B表的记录总数,无意义的结果集

两张表联合查询,要添加至少一个连接条件。因为查询时很难避免两张表出现相同名字的字段,为了解决这个问题,可以

使用 表名.字段 来确认

若别名比较长,柯表名叫上别名来简化

SELECT e.ename,e.sal,d.dname,e.deptno FROM emp e,dept d WHERE e.deptno = d.pdeptno;

SELECT e.ename,e.sal,d.dname,e.deptno FROM emp e JOIN dept d ON(d.deptno=e.deptno);

SQL89标准时,弊端在于,连接条件与过滤条件都要写在where子句中,可读性相对较差,尤其多表查询加上过滤条件多的时候。

SELECT e.ename,d.DNAME  FROM emp e,dept d WHERE d.DEPTNO=e.DEPTNO AND d.DNAME='SALES';

SQL92建议多表连接用 用SELECT ... FROM JOIN...ON ...WHERE(只写条件)

SELECT e.ename,d.dname FROM emp e JOIN dept d ON e.deptno = e.deptno WHERE d.dname ='SALES'

SQL92标准建议我们在夺标连接时使用内连接形式

这样会发现,连接条件单独定义在ON子句中,而过滤条件卸载WHERE子句中,可读性比较强

SELECT e.ename,d.dname FROM emp e JOIN dept d ON e.deptno=d.deptno WHERE d.dname='SALES';

外连接

自连接

当前表中的数据关联 表中的其他数据。就构成了自连接。通常表被设计成自连接是为了表示同类型数据有存在上下级关系(树状结构的数据通常设计为自连接。)

例如:淘宝网的类别。就被设计为自连接。

设计表的时候以及关联关系时通常有两个字段很重要,他们被称为:主键外键

通常每张表的第一个字段就是主键,主键保存的数据一般与数据无关系,他只是用来标识每条数据的唯一性,所以主键的要求是

存储的值非空且唯一。

外键,用来保存关系表中记录的主键的值,例如

部门表中有一个字段叫做dept,它用来保存dept表中某条记录主键的值那么emp表的dept就是外键

在关联关系中,保存外键的表通常是“一对多”中“多”的一方

本文永久更新链接地址

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

See all articles