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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

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

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

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

The Mistral open source code model takes the throne! Codestral is crazy about training in over 80 languages, and domestic Tongyi developers are asking to participate! The Mistral open source code model takes the throne! Codestral is crazy about training in over 80 languages, and domestic Tongyi developers are asking to participate! Jun 08, 2024 pm 09:55 PM

Produced by 51CTO technology stack (WeChat ID: blog51cto) Mistral released its first code model Codestral-22B! What’s crazy about this model is not only that it’s trained on over 80 programming languages, including Swift, etc. that many code models ignore. Their speeds are not exactly the same. It is required to write a "publish/subscribe" system using Go language. The GPT-4o here is being output, and Codestral is handing in the paper so fast that it’s hard to see! Since the model has just been launched, it has not yet been publicly tested. But according to the person in charge of Mistral, Codestral is currently the best-performing open source code model. Friends who are interested in the picture can move to: - Hug the face: https

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles