Home Database Mysql Tutorial 数据库查询 方式轻松走进数据库系列之十三

数据库查询 方式轻松走进数据库系列之十三

Jun 07, 2016 pm 03:15 PM
sql one Thirteen database Way Inquire series language easy

Sql 语言是一种数据化查询语言。学习 sql 语言的目的就是对数据据库进行操作。 我们来看一下数据库中 SQL 语句的一些查询方式。 一、简单查询 简单查询即从单个表中查询数据。 语法 select 列名 1 ,列名 2, …… |* 要查询的列,如果是某个表的全部列那么可

Sql语言是一种数据化查询语言。学习sql语言的目的就是对数据据库进行操作。

我们来看一下数据库中SQL语句的一些查询方式。

 

一、简单查询

简单查询即从单个表中查询数据。

语法

select列名1,列名2,……  |*         要查询的列,如果是某个表的全部列那么可以写成*

From表名                   要查询的表。

Where条件                                          查询所依据的条件

Group by列名                结果集分组依据

Having 列名                对结果集分组后再次进行条件查询

Order by列名【adc|desc    结果集排序顺序的依据

 

二、高级查询

 

多表查询,分组查询,统计查询,嵌套查询

 

1、多表查询:连接查询,合并查询。

1)、连接查询:在多表查询中,通过每个表之间共同列的关联性来查询数据。

连接类型又分为:内连接(inner),外连接(outer),交叉连接(cross)。

具体格式如下表:

 数据库查询 方式轻松走进数据库系列之十三


语法select 列名1,列名2,……

From表名1  as别名】 连接类型> join 表名2>  as别名】

On (连接条件)

Where查询条件

 

注意

1、查询多表中有重复的列可以加表名以区分。

2、可以为表名设定别名。

3、选择列表可以是查询所涉及表中的任何列,可以不包含连接条件列。

4、连接条件中比较的列不必同名;数据类型相同或兼容,不兼容要用cast函数显示转换数据类型。

5ntext .text.image类型的列无法直接作为连接列,可以用substringntext.text.image列上间接连接表。

6、内连接关键字inner可以省略。

7、多表连接中,如果是两个相同的表叫做自连接。

8on后的连接条件可以放在where子句中,此时,连接子句和条件子句中加and

 

2)、合并查询

合并查询:使用 union将多个表的查询结果合并为一个结果集。要求两个查询结果必须:列数据类型兼容、列数目和顺序一致。

语法

查询1>

Unionall         all 指合并后结果集中包含所有的行,有重复行。

查询2>

查询1这类句子包含的可以是select语句,可以是一个已经存在的查询名,还可是一个已经存在的表。

合并的结果集列名是第一个select选择出的列名。

 

 

 

2、分组查询

1group by 子句。根据group by后的条件对查询结果进行分组。

2Having子句。group by分组后分别在组内进行条件查询。

having子句是依赖groupby而存在的。有having必有group by ,groupby 不必定有having

 

3、统计查询

通过使用聚合函数,在结果集中分别汇总查询出来的信息。如:求和,求平均值等。

基本语法

Select  聚合函数名1(列名1),聚合函数名2(列名2)……

From 表名

 

4、嵌套查询

嵌套查询是在一个select查询中嵌套一个select子查询块。子查询可以嵌套在selectinsertupdatedelete语句的wherehaving子句中。可以多层嵌套。

嵌套一般分为:in嵌套,运算符嵌套,exists嵌套。

 

 

三、存储查询的结果

 

存储结果集 into

Select  列名1,…… into 表名                    此处表名可以是将要新建立的表名也可以是已经存在的表

From 表名

Where条件语句

 

对结果集进行修改

Update|delete  结果集所在的表名

Set 旧列=新列

From   结果集所在的表名

Where 条件语句


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

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).

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

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];

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

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

Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Aug 22, 2024 pm 06:47 PM

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions Aug 29, 2024 pm 03:33 PM

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

See all articles