高级查询
子查询,在一个SQL语句中嵌套另一个SQL语句称为子查询 需求:查询商品类别为“图书”的所有商品的id、商品名称、商品价 select id,name,price from es_product where sort_id = (select idfrom es_sort where sortname = ‘图书’) 子查询的结果可能不是一行
子查询,在一个SQL语句中嵌套另一个SQL语句称为子查询
需求:查询商品类别为“图书”的所有商品的id、商品名称、商品价格
select id,name,price from es_product where sort_id = (select idfrom es_sort where sortname = ‘图书’)
子查询的结果可能不是一行,可能返回多行查询结果的子查询,这样情况就不能使用=、<、>、<=等那些运算符了。
ROWNUM是oracle数据库从数据文件或缓冲区中读取数据的顺序。简单的说,ROWNUM是对符合条件结果的序列号,即先查到结果集之后再加上去的一个列。他总是从1开始的。物理上并不真正存在。
示例:查询商品表中前五条商品记录的id、商品名称、以及上架日期
select id,name,saledate from es_product where rownum <=5;
示例:查询商品列表中最新上架的前五条商品的id、商品名称以及上架日期
select id, name, saledate from (select * from es_product order by saledate DESC) where rownum <= 5
分页查询
需求:分页显示商品id、商品名称、上架时间、每页显示四条,请编写查询第二页商品信息的sql语句
--将rownum固化
select id,name,saledate,rownum rn from es_product
--通过限制rn的范围取记录
select id,name,saledate from(select id,name,saledate,rownum rn from es_product) wherern>=5 and rn<=8
按日期降序,分页显示商品id、商品名称、上架时间、每页显示4条
请编写查询第二页商品信息的sql语句
select id,name,saledate from (select id,name,saledate,rownum rn from(select * fromes_product order by saledate DESC)) where rn>=5 and rn <=8
链接查询
需求:查询商品类别为“图书”的所有商品的id、商品名称、商品价格、商品所属分类名称
select es_product.id,name,price,sortname from es_product,es_sort wherees_product.sort_id = es_sort.id and es_sort,sortname = ‘图书’;
查询字段中id前面添加了es_product.是因为:当被连接的多个表中存在同名字段时,则必须在该字段前面加上“表名.”做为前缀
子查询和链接查询区别:子查询并不要求两个表有相关字段,只要得到的子查询的结果集用于父查询。而连接查询。则必须要求两个表有相关字段。当查询的列来自多个表时,可以使用表连接查。
表连接语法:select要查询的字段 from表1,表2……where链接条件
非等值连接案例:查询所有单价介于20-35元之间的商品名称、价格、库存量以及商品类别名称。
需求:查询所有用户的用户姓名、电话(此处为订单表中的收货人电话)、订单号、订购日期以及订单状态
select a.realname,b.tel,b.id,b.createtime,b.status from es_user a,es_order b whereb.user_id = a.id
当某些不满足条件的列也需要显示出来。也就是说,只限制其中一个表的行,而不限制另外一个表的行时需要使用外连接。Oracle中使用+表示外连接,或者使用LEFT OUTER JOIN(左连接)RIGHT OUTER JOIN(右外连接) FULL OUTER JOIN(全外连接)
需求:查询所有用户的用户姓名、电话(此处为订单表中的收货人电话)、订单号、订购日期以及订单状态。可以进行如下修改 :
select a.realname,b.tel,b.id,createtime,status fromes_user a,left out join es_order b on a.id =b.user_id
左外连接的语法:
select 多表查询的列
from 左表 left [outer] join 右表
on 多表连接的条件
写法二:
select a.realname,b.tel,b.id,createtime,status from es_user a,es_order b where a.id = b.user_id(+)
将加号写在右边表示以左边为准,右边补齐。
集合查询:将多个查询组合到一个查询中去
对于Oracle,交集就是返回两个查询共有的记录,关键字是INTERSECT;并集是返回各个查询的所有记录,关键字是UNION或者UNION ALL;补集是返回第一个查询记录检索出的记录减去第二个查询检索出的记录之后剩余的记录,关键字是MINUS。
UNION和UNION ALL区别:UNION和UNION ALL都可以用在结果查询的“并集”操作,不过UNION ALL比UNION效率高,因为UNION查询出的内容剔除重复行,同时默认按第一个查询的第一列升序排序。而UNION ALL不做这些。
注意:分组以后筛选使用HAVING关键字,而不能使用WHERE。
示例1:获取一次订单满200元或者累计满500元的用户
select user_id from es_order where totalamount>200 UNION select user_id fromes_order group by user_id HAVING sum(totalamount) >500
示例2:获取一次订单满200并且订单累计满500元的用户
select user_id from es_order where totalamount>200 INTERSECT select user_id fromes_order group by user_id HAVING sum(totalamount) >500
示例3:获取没有订购过商品ID
select id from es_product MINUS selectprod_id from es_ordertail

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Nested Generic Functions Generic functions in Go 1.18 allow the creation of functions that apply to multiple types, and nested generic functions can create reusable code hierarchies: Generic functions can be nested within each other, creating a nested code reuse structure. By composing filters and mapping functions into a pipeline, you can create reusable type-safe pipelines. Nested generic functions provide a powerful tool for creating reusable, type-safe code, making your code more efficient and maintainable.

Database technology competition: What are the differences between Oracle and SQL? In the database field, Oracle and SQL Server are two highly respected relational database management systems. Although they both belong to the category of relational databases, there are many differences between them. In this article, we will delve into the differences between Oracle and SQL Server, as well as their features and advantages in practical applications. First of all, there are differences in syntax between Oracle and SQL Server.

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same
