jpa 查询编写sql_学习编写基本SQL查询

本文介绍了SQL语言的基础知识,包括T-SQL、关系数据库和数据表的概念。通过实例展示了如何使用SELECT语句、WHERE子句进行数据筛选,以及ORDER BY和DISTINCT子句进行数据排序和去重。文章还提供了简单的SQL查询示例和练习题,帮助读者掌握基本的SQL查询技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jpa 查询编写sql

Essentially, SQL language allows us to retrieve and manipulate data on the data tables. In this article, we will understand and gain the ability to write fundamental SQL queries. At first, we will take a glance at the main notions that we need to know about in order to write database queries.

本质上,SQL语言允许我们检索和操作数据表上的数据。 在本文中,我们将了解并获得编写基本SQL查询的能力。 首先,我们将浏览一下为了编写数据库查询而需要了解的主要概念。

什么是T-SQL? (What is the T-SQL?)

SQL is the abbreviation of the Structured Query Language words and, it is used to query the databases. Transact-SQL (T-SQL) language is an extended implementation of the SQL for the Microsoft SQL Server. In this article, we will use the T-SQL standards in the examples.

SQL是结构化查询语言单词的缩写,用于查询数据库。 Transact-SQL(T-SQL)语言是Microsoft SQL ServerSQL的扩展实现。 在本文中,我们将在示例中使用T-SQL标准。

什么是关系数据库? (What is a Relational Database?)

Most simply, we can define the relational database as the logical structure in which data tables are kept that can relate to each other.

最简单的说,我们可以将关系数据库定义为逻辑结构,其中保持可以相互关联的数据表。

什么是数据表? ( What is a Data Table?)

A  table is a database object that allows us to keep data through columns and rows. We can say that data tables are the main objects of the databases because they are holding the data in the relational databases.

表是一个数据库对象,它使我们能够保留列和行中的数据。 我们可以说数据表是数据库的主要对象,因为它们将数据保存在关系数据库中。

Assume that we have a table that holds the history class students’ details data. It is formed in the following columns.

假设我们有一个表保存历史课学生的详细数据。 它在以下几列中形成。

Name: Student name

姓名:学生姓名

SurName: Student surname

姓:学生姓

Lesson: Opted lesson

课程:选择的课程

Age: Student age

年龄:学生年龄

PassMark: Passing mark

PassMark:通过标记

Student table data illustration

We will use this table in our demonstrations in this article. The name of this data table is Student.

在本文的演示中,我们将使用此表。 该数据表的名称是Student。

我们的第一个查询:SELECT语句 (Our First Query: SELECT Statement)

The SELECT statement can be described as the starting or the zero point of the SQL queries. The SELECT statement is used to retrieve data from the data tables. In the SELECT statement syntax, at first, we specify the column names and separate them with a comma if we use a single column we don’t use any comma in the SELECT statements. In the second step, we write the FROM clause and as a last, we specify the table name. When we consider the below example, it retrieves data from Name and Surname columns, the SELECT statement syntax will be as below:

SELECT语句可以描述为SQL查询的起点或零点。 SELECT语句用于从数据表中检索数据。 首先,在SELECT语句语法中,我们指定列名,并使用逗号分隔它们(如果使用单个列,则在SELECT语句中不使用任何逗号)。 第二步,编写FROM子句,最后,指定表名。 当我们考虑下面的示例时,它从NameSurname列检索数据, SELECT语句的语法如下:

SELECT Name
    ,SurName
FROM  Student

Basic SQL Queries: SELECT statement

If we want to retrieve data from only the Name column, the SELECT statement syntax will be as below:

如果只想从“ 名称”列中检索数据,则SELECT语句的语法如下:

SELECT Name
FROM Student

Basic SQL Queries: SELECT statement for single column

Tip: We can easily try all these examples in this article by ourselves in the SQL Fiddle over this link. After navigating to the link, we need to clear the query panel and execute the sample queries.

提示:我们可以在SQL Fiddle中通过此链接轻松地尝试本文中的所有这些示例。 导航到链接后,我们需要清除查询面板并执行示例查询。

How to execute a query in SQL Fiddle

The asterisk (*) sign defines all columns of the table. If we consider the below example, the SELECT statement returns all columns of the Student table.

星号( * )定义表的所有列。 如果考虑以下示例,则SELECT语句返回Student表的所​​有列。

SELECT *
FROM Student

Using the asterisk ( * ) sign in SELECT statement
  • Tip:小费:
  • SELECT statements. This usage type causes the consume more IO, CPU and Network cost. As a result, if we don’t need all columns of the table in our queries we can abandon to use asterisk sign and only use the necessary columnsSELECT语句中使用星号(*)。 这种用法类型导致消耗更多的IO,CPU和网络成本。 结果,如果我们不需要查询中表格的所有列,我们可以放弃使用星号,而只使用必要的列

筛选数据:WHERE子句 ( Filtering the Data: WHERE Clause)

WHERE clause is used to filter the data according to specified conditions. After the WHERE clause, we must define the filtering condition. The following example retrieves the students whose age is bigger and equal to 20.

WHERE子句用于根据指定条件过滤数据。 在WHERE子句之后,我们必须定义过滤条件。 以下示例检索年龄大于等于20的学生。

SELECT *
FROM  Student 
WHERE Age >=20

Basic SQL Queries: WHERE Clause

LIKE operator is a logical operator that provides to apply a special filtering pattern to WHERE condition in SQL queries. Percentage sign (%) is the main wildcard to use as a conjunction with the LIKE operator. Through the following query, we will retrieve the students whose names start with J character.

LIKE运算符是一种逻辑运算符,提供对SQL查询中的WHERE条件应用特殊的过滤模式。 百分号( )是与LIKE运算符结合使用的主要通配符。 通过以下查询,我们将检索名称以J字符开头的学生。

SELECT *
FROM  Student
WHERE Name LIKE 'J%'

LIKE operator usage in a WHERE clause

IN operator enables us to apply multiple value filters to WHERE clause. The following query fetches the students’ data who have taken the Roman and European History lessons.

IN运算符使我们可以将多个值过滤器应用于WHERE子句。 以下查询获取参加过罗马和欧洲历史课程的学生的数据。

SELECT *
FROM  Student 
WHERE Lesson IN ('Roman History','European History')

IN operator usage in a WHERE clause

The BETWEEN operator filters the data that falls into the defined begin and end value. The following query returns data for the students whose marks are equal to and bigger than 40 and smaller and equal to 60.

BETWEEN运算符过滤落入定义的开始和结束值的数据。 以下查询返回分数等于或大于40且小于等于60的学生的数据。

SELECT *
FROM  Student
WHERE PassMark BETWEEN 40 AND 60

BETWEEN operator usage in a WHERE clause

排序数据:ORDER BY语句 (Sorting the Data: ORDER BY Statement)

ORDER BY statement helps us to sort the data according to the specified column. The result set of the data can be sorted either ascending or descending. ASC keyword sorts the data in ascending order and the DESC keyword sorts the data in descending order. The following query sorts the students’ data in descending order according to the PassMark column expressions.

ORDER BY语句帮助我们根据指定的列对数据进行排序。 数据的结果集可以升序或降序排序。 ASC关键字以升序对数据进行排序,而DESC关键字以降序对数据进行排序。 以下查询根据PassMark列表达式以降序对学生的数据进行排序。

SELECT *
FROM  Student
ORDER BY PassMark DESC

Basic SQL Queries: ORDER BY statement

By default ORDER BY statement sorts data in ascending order. The following example demonstrates the default usage of the ORDER BY statement.

默认情况下, ORDER BY语句按升序对数据进行排序。 下面的示例演示了ORDER BY语句的默认用法。

SELECT *
FROM  Student 
ORDER BY PassMark

Sorting the data in ascending order with the help of the ASC keyword.

消除重复数据:DISTINCT子句 (Eliminating the Duplicate Data: DISTINCT Clause)

The DISTINCT clause is used to eliminate duplicate data from the specified columns so the result set is populated only with the distinct (different) values. In the following example, we will retrieve Lesson column data, however, while doing so, we will retrieve only distinct values with the help of the DISTINCT clause

DISTINCT子句用于从指定的列中消除重复的数据,因此仅使用不同的(不同的)值填充结果集。 在下面的示例中,我们将检索Lesson列数据,但是在这样做的同时,我们将借助DISTINCT子句仅检索不同的值。

SELECT *
FROM Student
WHERE Age >= 20

Basic SQL Queries: DISTINCT clause

As we can see, the DISTINCT clause has removed the multiple values and these values added to the result set only once.

如我们所见, DISTINCT子句删除了多个值,并且这些值仅一次添加到结果集中。

测验 (Quiz)

In this section, we can test our learnings.

在本节中,我们可以测试我们的学习。

Question – 1:

问题– 1:

Write a query that shows student name and surname whose ages are between 22 and 24.

编写查询以显示年龄在22至24岁之间的学生姓名和姓氏。

Answer :

答:

SELECT Name, 
    SurName
FROM Student
WHERE Age BETWEEN 22 AND 24

Question – 2:

问题2:

Write a query that shows student names and ages in the descending order who takes Roman and Ancient History lessons.

编写一个查询,以降序显示参加罗马和古代历史课程的学生姓名和年龄。

Answer :

答:

SELECT Name, 
    SurName, 
    Age
FROM Student
WHERE lesson IN('Roman History', 'Ancient History')
ORDER BY Age DESC

结论 (Conclusion)

In this article, we learned how we can write the basic SQL queries, besides that we demonstrated usage of the queries with straightforward examples.

在本文中,我们学习了如何编写基本SQL查询,此外,我们还通过简单的示例演示了查询的用法。

翻译自: https://www.sqlshack.com/learn-to-write-basic-sql-queries/

jpa 查询编写sql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值