[Pro-test] Oracle query - subqueries, paging query (b)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43652509/article/details/86301674

ORACLE queries (subqueries, paging query)

A subquery

(A) single-row subquery (returns only one record)

1. queries January 2012 ledger recording water is greater than the average
select * from t_account where usenum > 
(select avg(usenum) from t_account where  year = '2012' and month = '01' );

result:
Here Insert Picture Description

(B) multiple-row subqueries (returned multiple records)

in operator
1. Query for the owners address number recorded 1, 3, 4
select * from t_owners  where addressid in (1,3,4) ;

result:
Here Insert Picture Description

2. The address query contains information owners "garden"
select * from t_owners where addressid in ( select id from t_address where name like '%花园%');

result:
Here Insert Picture Description

3. Query address does not contain information on the owners of the "garden"
select * from t_owners where addressid not in (select id from t_address where name like '%花园%');

result:
Here Insert Picture Description

4. Query 2012 ledger, the use of greater than March 2012. The maximum amount of the ledger data
select * from t_account where year = '2012' and usenum >
(select max(usenum) from t_account where year = '2012' and month = '03');

result:
Here Insert Picture Description

(C) nested subqueries (subquery then nested subqueries)

1. Query the owners records contain the name of the garden in the district of Haidian District
select * from t_owners ow 
where addressid in 
(select ar.id from t_area ar , t_address ad 
where ar.id = ad.areaid and ar.name = '海淀' and ad.name like '%花园%');

result:
Here Insert Picture Description

(Iv) scalar query (subquery result statement executed directly as a result of the main query shown) scalar query implementation: dual dedicated to presenting data

1. Inquiry and the sum of the average annual water consumption Ledger table annual water consumption of the user
-- 方式一
select sum(usenum),avg(usenum) from t_account ;
-- 方式二
select (select sum(usenum) from t_account) 年用水 ,  (select avg(usenum) from t_account)  年均用水 from dual;

result:
Here Insert Picture Description

(V) correlated subqueries (subqueries need to rely on the main query)

1. Query display owner number, owner name, address and type of owner
--  方式一
select os.id ,os.name,ad.name, ow.name from t_owners os ,t_address ad,t_ownertype ow;
--  方式二
select os.id , os.name 业主名 ,
(select name from t_address where id = os.addressid)  地址,
(select name from t_ownertype where id= os.ownertypeid) 业主类型
from t_owners os;

result:
Here Insert Picture Description

Second, paging query

(A) simple paging
1. Sort query ledger table T_ACCOUNT, 10 records per page
select * from t_account where Rownum <= 10;

result:
Here Insert Picture Description

1. Sort query ledger table T_ACCOUNT, page 11--20 records
select a.* from 
(select rownum rn,ac.* from t_account ac) a
where rn between 11 and 20;

result:
Here Insert Picture Description

(B) based on the sort of page
1. Based on sorting table paging query ledger T_ACCOUNT, page 11--20 records
select a.* from 
(select rownum r,t.* from 
        (select * from t_account order by money asc) t) a
where a.r between 11 and 20;

result:
Here Insert Picture Description

The following article personal recommendation
[pro-test] Oracle Database Installation and Configuration

[Pro-test] operation the Oracle Database

Create a [pro-test] Oracle table, modify and delete

[Pro-test] the Oracle database constraints

[Pro-test] the Oracle Data CRUD

[Pro-test] the Oracle Query - Query single table, a join query (a)

[Pro-test] Oracle query - subqueries, paging query (b)

[Pro-test] the Oracle queries - one-way function -PL / SQL, analysis functions, set operations (c)

How to use Oracle execution plan Explain Plan

[Pro-test] database optimization

Guess you like

Origin blog.csdn.net/qq_43652509/article/details/86301674