In this series of articles, we are going to learn about the basics of the MySQL database server. In this article, I am going to explain how we can query the data from the MySQL server using the SELECT statement.

在本系列文章中,我们将学习MySQL数据库服务器的基础知识。 在本文中,我将解释如何使用SELECT语句从MySQL服务器查询数据。

For the demonstration, I have installed MySQL 8.0 and MySQL Workbench on my workstation and also restored a demo database named sakila. This article helps you to install the MySQL server on Windows 10. You can download the sakila database from here, and the installation steps are provided in this article.

为了演示,我在工作站上安装了MySQL 8.0和MySQL Workbench,还还原了一个名为sakila的演示数据库。 本文帮助您安装在Windows 10.您MySQL服务器可以从下载sakila数据库在这里 ,并在提供的安装步骤此文章。

Now to understand the schema architecture of the tables of the sakila, we will generate a schema diagram by reverse-engineering the database.

现在,要了解sakila表的架构,我们将通过对数据库进行反向工程来生成架构图。

生成架构图 (Generate schema diagram )

To generate the schema diagram, Open MySQL workbench -> Connect to MySQL database engine -> From the menu bar, click on the database -> Select Reverse Engineer. See the following image:

要生成模式图,请打开MySQL工作台->连接到MySQL数据库引擎 ->从菜单栏中,单击数据库 ->选择反向工程师 。 见下图:

On the Reverse Engineer Database screen, choose the connection name from the Stored Connection drop-down box, choose the appropriate connection method (TCP/IP OR Named Pipe) from Connection Method drop-down box. Enter hostname or IP Address in the Hostname text box. Enter the appropriate port number in Port Number text box. And finally, enter the appropriate user name in the Username text box and click on Next. See the following image:

逆向工程数据库屏幕上,从所存储连接下拉框中连接的名称,然后从连接方法相应的连接方式(TCP / IP或命名管道 )下拉框。 在“主机名”文本框中输入主机名或IP地址。 在“端口号”文本框中输入适当的端口号。 最后,在用户名文本框中输入相应的用户名 ,然后单击下一步 。 见下图:

Now, MySQL database server tries to establish the connection between the reverse engineering wizard and server and fetch the information of the database hosted on the server. If it fails, then the error will be shown on Connect to DBMS and fetch the information screen. The connection is established successfully. See the following image. Click on Next.

现在,MySQL数据库服务器尝试在逆向工程向导和服务器之间建立连接,并获取服务器上托管的数据库的信息。 如果失败,则错误将显示在“ 连接到DBMS”并获取信息屏幕上。 连接成功建立。 参见下图。 单击下一步。

On the Select Schemas to Reverse Engineer screen, you can choose the database. The wizard will generate the schema diagram of the selected database. We want to generate the schema of the sakila database; hence, click on sakila and click on Next. See the following image:

在“ 选择要逆向工程的模式”屏幕上,可以选择数据库。 该向导将生成所选数据库的架构图。 我们要生成sakila数据库的模式; 因此,单击sakila ,然后单击Next。 见下图:

On retrieve and reverse engineer schema object screen, the wizard populates the schema object to the sakila database. If any occurs, which fetching the object, it displays on the screen. The schema is fetched successfully. Click on Next. See the following image:

在“检索和反向工程模式对象”屏幕上,向导将模式对象填充到sakila数据库。 如果发生任何事情(取回对象),它将显示在屏幕上。 架构已成功获取。 单击下一步。 见下图:

On Select objects to Reverse Engineer screen, you can choose the objects which you want to reverse engineer. We want to generate the diagram of the tables hence select Import MySQL table object and click on Execute. See the following image:

选择要逆向工程的对象屏幕上,可以选择要逆向工程的对象。 我们要生成表图,因此选择Import MySQL table object并单击Execute 。 见下图:

The reverse engineering process on the database starts. If it encounters any error, it will be displayed on the Reverse engineering process screen. The process was completed successfully. Click on Next. See the following image:

数据库的逆向工程过程开始。 如果遇到任何错误,它将显示在“逆向工程过程”屏幕上。 该过程已成功完成。 单击下一步。 见下图:

On the Reverse Engineering Result screen, you can see the details of the reverse-engineered objects. Click on Finish to close the dialog box. See the following image:

在“ 反向工程结果”屏幕上,您可以看到反向工程对象的详细信息。 单击完成关闭对话框。 见下图:

Once the process completes, you can see the schema diagram of the sakila database that has been created. The diagram is in the EER tab of MySQL workbench. If you want to use it later, you can save this diagram. See the following image:

该过程完成后,您可以看到已创建的sakila数据库的架构图。 该图位于MySQL工作台的EER选项卡中。 如果要以后使用,可以保存此图表。 见下图:

Now, as the diagram has been prepared, we can use it to understand the schema structure of the database and use the tables to understand the concept of MySQL database programming. First, let me explain about the SELECT statement.

现在,在准备好图之后,我们可以使用它来了解数据库的架构结构,并使用表来了解MySQL数据库编程的概念。 首先,让我解释一下SELECT语句。

SELECT语句简介 (Introduction to the SELECT statement)

The SELECT statement is used to populate data from any table of the MySQL database server. It is neither a DML (Data modification language) or DDL (Data definition language). It is a limited form of DML statement that is used only to populate the data from the database. The basic syntax of the SELECT statement is following

SELECT语句用于从MySQL数据库服务器的任何表填充数据。 它既不是DML(数据修改语言)也不是DDL(数据定义语言)。 它是DML语句的一种受限形式,仅用于填充数据库中的数据。 SELECT语句的基本语法如下

Select
<column_1>,
<column_2>,..
From Table;

In the syntax, the keyword SELECT keyword is to instruct the database to retrieve the data. The second segment is the list of columns or the expressions which you want to retrieve in the result set. Here you can specify the list of columns or the expressions, or you can specify the star(*). Now, when we specify the star(*), then the query populates all the columns.

在语法中,关键字SELECT关键字用于指示数据库检索数据。 第二段是要在结果集中检索的列或表达式的列表。 您可以在此处指定列或表达式的列表,也可以指定star(*)。 现在,当我们指定star(*)时,查询将填充所有列。

Finally, in the FROM keyword, you can specify the name of the table or the view. If you want to filter the data or sort the data, the keywords will be placed after the name of the table or view.

最后,在FROM关键字中,您可以指定表或视图的名称。 如果要过滤数据或对数据进行排序,关键字将放在表或视图的名称之后。

In the syntax, the semicolon is used as a statement delimiter. The semicolon is considered as an end of the query. If you want to get the result of multiple queries, then you must specify semicolon at the end of an individual query. When we specify it at the end of multiple queries, MySQL executes them individually and generates separate resultsets for them.

在语法中,分号用作语句定界符。 分号被认为是查询的结尾。 如果要获取多个查询的结果,则必须在单个查询的末尾指定分号。 当我们在多个查询的末尾指定它时,MySQL会分别执行它们并为它们生成单独的结果集。

  • Note: Many users ask how we can run multiple SELECT statements in MySQL Workbench. When we want to use multiple SELECT statements in the query, you must specify semicolon at the end of the query. The result set of the queries will be displayed in multiple tabs of the output window.

    注意:许多用户问我们如何在MySQL Workbench中运行多个SELECT语句。 当我们要在查询中使用多个SELECT语句时,必须在查询末尾指定分号。 查询的结果集将显示在输出窗口的多个选项卡中。

使用SELECT语句检索表的特定列数据 (Using the SELECT statement to retrieve specific columns data of the table )

For example, I want to populate only the first_name, last_name, and email column of the customer table of the sakila database. The query should be written as follows:

例如,我只想填充sakila数据库的客户表的first_name,last_name和email列。 该查询应编写如下:

use sakila;
select first_name, last_name, email from customer;

The output is below

输出如下

使用SELECT语句检索表的所有列 (Using the SELECT statement to retrieve all the columns of the table )

For example, I want to populate all columns of the customer table of the sakila database. The query should be written as follows:

例如,我要填充sakila数据库的customer表的所有列。 该查询应编写如下:

use sakila;
select * from customer;

The output is shown below:

输出如下所示:

关于在Select语句中使用开始(*)的几点注意事项 (Few notes about using start (*) in Select statement)

  1. SELECT * FROM table unless it is necessary. SELECT * FROM表。 SELECT * FROM always generates an unnecessary IO on the database. For example, you have a table that has a column that stores BLOB data, and when you use SELECT * FROM总是在数据库上生成不必要的IO。 例如,您有一个表,该表的列存储BLOB数据,当您对该表使用SELECT * SELECT * FROM for that table, query populates the BLOB column as well, which generates a tremendous amount of IO FROM时 ,查询也会填充BLOB列,这会产生大量的IO
  2. Suppose, you have developed an application, and within the application, you store the output of the query in a dataset, and you are using the index of columns. Now due to business requirements, you have to add more columns in the table. In such cases, the index of the columns will be changed, so in the dataset, you will get the unexpected resultset 假设您已经开发了一个应用程序,并且在该应用程序内,将查询的输出存储在数据集中,并且正在使用列索引。 现在,由于业务需求,您必须在表中添加更多列。 在这种情况下,列的索引将被更改,因此在数据集中,您将获得意外的结果集
  3. user_id column is 0, an index of the user_id列的索引为0, first_name column is 2. Now someone adds the first_name列的索引为2。现在有人在索引位置1添加了password column at index location 1. Now, based on the index of columns defined in the dataset, the application displays the data in textboxes. Now, when the web pages load, in the textbox, which displays the first name of the user, it will show the password of the user. It is one of the rare mistakes, but might happens for the people who are developing the application for the first time 密码列。现在,基于数据集中定义的列的索引,应用程序显示数据在文本框中。 现在,在加载网页时,在显示用户名的文本框中,将显示用户密码。 这是罕见的错误之一,但对于第一次开发应用程序的人可能会发生
  4. Another example is an ad-hoc query. If you use SELECT * FROM in the query, it will show the password column as well. It is a common mistake, but nowadays, the MySQL database server is smart enough to hide the sensitive information by using the data masking feature. You can read more about it from this article, Enterprise Data Masking in MySQL
  5. 另一个示例是即席查询。 如果在查询中使用SELECT * FROM ,它将同时显示密码列。 这是一个常见的错误,但是如今,MySQL数据库服务器已经足够聪明,可以使用数据屏蔽功能来隐藏敏感信息。 您可以从本文“ MySQL中的企业数据屏蔽”中了解更多信息。

摘要 (Summary)

In this article, I have explained how we can generate the schema of an existing MySQL database using MySQL Workbench. Moreover, I have also explained the SELECT statement in the MySQL database server. In the next article, I am going to cover how we can filter and sort the data from the table. Stay tuned..!!

在本文中,我已经解释了如何使用MySQL Workbench生成现有MySQL数据库的架构。 此外,我还解释了MySQL数据库服务器中的SELECT语句。 在下一篇文章中,我将介绍如何对表中的数据进行过滤和排序。 敬请关注..!!

目录 (Table of contents)

Learn MySQL: Querying data from MySQL server using the SELECT statement
Learn MySQL: What is pagination
Learn MySQL: Sorting and Filtering data in a table
学习MySQL:使用SELECT语句从MySQL服务器查询数据
学习MySQL:什么是分页
学习MySQL:对表中的数据进行排序和过滤

翻译自: https://www.sqlshack.com/learn-mysql-querying-data-from-mysql-server-using-the-select-statement/

学习MySQL:使用SELECT语句从MySQL服务器查询数据相关推荐

  1. mysql com_select_mysql学习一之SELECT语句

    完整语法 先给一下完整的语法,后面将逐一来讲解. 基础语法 SELECT 字段列表 FROM 数据源; 完整语法 select 去重选项 字段列表 [as 字段别名] from 数据源 [where子 ...

  2. 一文搞懂select语句在MySQL中的执行流程!

    MySQL作为互联网行业使用最多的关系型数据库之一,与其免费.开源的特性是密不可分的.然而,很多小伙伴工作了很多年,只知道使用MySQL进行CRUD操作,这也导致很多小伙伴工作多年后,想跳槽进入大厂, ...

  3. 原来select语句在MySQL中是这样执行的!看完又涨见识了!这回我要碾压面试官!

    大家好,我是冰河~~ MySQL作为互联网行业使用最多的关系型数据库之一,与其免费.开源的特性是密不可分的.然而,很多小伙伴工作了很多年,只知道使用MySQL进行CRUD操作,这也导致很多小伙伴工作多 ...

  4. MySQL中SELECT语句简单使用

    MySQL中SELECT语句简单使用 最近开始复习mysql,查漏补缺吧. 关于mysql 1.MySQL不区分大小写,但是在MySQL 4.1及之前的版本中,数据库名.表名.列名这些标识符默认是区分 ...

  5. mysql一秒查询次数_单个select语句实现MySQL查询统计次数

    单个select语句实现MySQL查询统计次数 单个select语句实现MySQL查询统计次数的方法用处在哪里呢?用处太多了,比如一个成绩单,你要查询及格得人数与不及格的人数,怎么一次查询出来? My ...

  6. MySQL使用SELECT 语句不加ORDER BY默认是如何排序的?

    大家好,我是阿飞云 怕什么真理无穷,进一步有近一步的欢喜 记录一个MySQL查询排序的问题,一个SQL语句没有加order by,那么查询出来的结果到底是按照什么规则排序的呢?查询了网上的一些资料,分 ...

  7. mysql查询单表的销售额_MYSQL select语句的单表查询

    MYSQL select语句的单表查询 发布时间:2020-05-29 13:19:48 来源:亿速云 阅读:131 作者:Leah 这篇文章运用了实例代码展示select语句的单表查询的用法,代码非 ...

  8. sql select 语句_学习SQL:SELECT语句

    sql select 语句 The SELECT statement is probably the most important SQL command. It's used to return r ...

  9. MySQL 实现多张无关联表查询数据并分页

    MySQL 实现多张无关联表查询数据并分页 1.功能需求 在三张没有主外键关联的表中取出自己想要的数据,并且分页. 2.数据库表结构 水果表: 坚果表: 饮料表: 主要用UNION AL UNION ...

最新文章

  1. winform 配置文件的加密解密
  2. MySQL事务隔离级别的实现原理
  3. 虚拟机性能测试:八 性能分析—Windows体验指数
  4. 表格占据整个页面_excel转换为pdf6个示例,在多个工作表、表格和空白页中进行选择...
  5. PHP过滤常用标签的正则表达式
  6. 剑指offer——面试题8:旋转数组的最小数字
  7. 简单易懂的PHP的命名空间以及配合use的使用
  8. Integer与int的种种比较你知道多少?
  9. 推荐Android中两个很好用的banner,可无限轮播,可使用第三方图片加载框架加载网络图...
  10. 2019最新版Eclipse下载与安装
  11. 《IT行业职位介绍-搞懂高级程序员、架构师、技术总监、CTO从薪资到技能的区别》
  12. 搜狗站长html标签验证,教你把企业网站添加到搜狗站长平台
  13. 计算机网络【最终版】
  14. 耗子大叔分享的软件设计原则
  15. 人力资源管理信息系统
  16. linux默认端口范围是多少?
  17. 【2019年秋PAT乙级考试】回顾 +题解+思路2019-9-8
  18. 超详细Redis入门教程——Redis 的安装与配置
  19. 微服务架构之公共模块式中创建API接口统一返回结果ApiResult
  20. java熔断器_一种熔断器的实现方法与流程

热门文章

  1. 整个技术架构结构设计
  2. 还来一篇说下json_value 以及 json_query 的应用 (3)
  3. JAVA开发第一步——JDK 安装
  4. 《HTML5 从入门到精通--7.6.3 单元格垂直跨度——rowspan》
  5. 用C#实现DES加密解密解决URL参数明文的问题
  6. BigBrother服务器端管理脚本_Bash
  7. LeetCode(235)——二叉搜索树的最近公共祖先(JavaScript)
  8. 数据结构---AVL树调整方法(详)
  9. python map lambda 分割字符串_[转] Python特殊语法:filter、map、reduce、lambda
  10. 剑指 Offer II 036. 后缀表达式