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:通过标记

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

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

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

SELECT Name
FROM Student

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中通过此链接轻松地尝试本文中的所有这些示例。 导航到链接后,我们需要清除查询面板并执行示例查询。

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
  • 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

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%'

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

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

排序数据: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

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

消除重复数据: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

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

jpa 查询编写sql_学习编写基本SQL查询相关推荐

  1. 【SAP ABAP学习资料】SQL查询分析器——程序代码

    运行效果 超级好用的SQL查询分析器,直接输入sql语句,点击执行就可以看到查询数据,还可以生成数据结构代码 程序代码 代码太多,打包放百度网盘了,链接永久有效 百度网盘,提取码:sap1

  2. sql 上亿 查询_在文件上使用 SQL 查询的示例

    [摘要] 本文介绍直接用 SQL 查询文件数据的各种情况,并用 esProc SPL 举例实现.请点击在文件上使用 SQL 查询的示例了解详情 在数据分析业务中经常要处理数据文件.我们知道,对于数据库 ...

  3. mysql查询sql中日期为当天的记录_SQL 查询当天,本月,本周的记录 sql 查询日期...

    SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111)   ORDE ...

  4. mysql怎么添加查询时间限制_mysql如何限制sql查询时间,

    mysql如何限制sql查询时间MySQL如何限制结构化查询语言查询时间,关系型数据库限制结构化查询语言查询时间的方法:1.查询今天,代码为[选择*表名where to_days(时间字段名)=to ...

  5. mysql中查询的第一名_使用SQL查询每科第一名及最后一名(学科、姓名、分数)...

    前段时间面试遇到被对方现场考察SQL水平的情况,现场写出来了但被面试官告知不正确,今天分享一下正确写法,一起共勉. 题目:使用SQL查询每科第一名及最后一名(学科.姓名.分数) 自己创建了下表以方便演 ...

  6. mysql查询是否用index_如何查看sql查询是否用到索引(mysql)

    alter database sync default character set utf8mb4;//修改数据库的字符集alter table sync.credit_creditchannel d ...

  7. sql server查询历史进程_学习笔记 | SequoiaDB SQL查询语句执行过程

    本篇笔记将为大家介绍 SequoiaDB 巨杉数据库查询 SQL 语句的执行过程,以及查询语句执行过程中实例层.协调节点.编码节点.数据节点各自承担的功能. 应用程序或用户想要从数据库查询需要的数据, ...

  8. mysql根据学号或是姓名查询_SQL学习之MySQL SQL查询作业答案

    #1.查询所有的课程的名称以及对应的任课老师姓名 SELECT course.cname, teacher.tname FROM course INNER JOIN teacher ON course ...

  9. 查询计算机系的所有教师,用sql查询所有计算机系的教师编号和姓名

    (1):答:select st.姓名,st.性别 from Student st,Sclass sc,Class c where st.学号=sc.学号 and sc.课程编号=c.课程编号 and ...

最新文章

  1. 归并排序详解(python实现)
  2. Java集合框架:LinkedList
  3. 用jquery实现html5的placeholder功能
  4. [机器学习] 推荐系统之协同过滤算法(转)
  5. pycharm中如何正确配置pyqt5
  6. 常见音视频格式(转载)
  7. NOIP2001-普及组复赛-第2题-最大公约数和最小公倍数问题
  8. 一些css3简单的练习
  9. SQL Server CTE 递归查询全解
  10. 鸿蒙OS 生成密钥和证书请求文件
  11. 企业版mysql怎么装_RedHat企业版5.5下Mysql的安装与配置
  12. 自媒体新手拍视频从哪开始入手?
  13. 泰安虚拟服务器怎么样,泰安S5353:搭建您最满意的服务器
  14. NodeJS连接MySQL
  15. 体验式培训之“盲人与哑巴”
  16. VS2015产品激活密钥
  17. 海思H.265解码库破解
  18. Docker硬盘空间使用分析与清理
  19. SQL SERVER 远程主机强迫关闭一个现有连接
  20. 一张图解释什么是遗传算法_通俗易懂地解释遗传算法

热门文章

  1. MYSQL中的日期转换
  2. java 多线程,线程安全等定义
  3. ltp-ddt realtime_cpu_load涉及的cyclictest 交叉编译
  4. PostgresException: 42883: function ifnull(integer, integer) does not exist
  5. C语言学习系列(六)基本语法
  6. T2 Funcin T1,out T2(T1 arg)
  7. 由浅入深解释JS执行机制 EventLoop
  8. 创建react项目并启动出现的报错及解决
  9. LeetCode(589)——N叉树的前序遍历(JavaScript)
  10. 句句真研—每日长难句打卡Day20