Introduction to MySQL syntax organization

coldplay.xixi
Release: 2021-04-14 09:54:14
forward
2697 people have browsed it

Introduction to MySQL syntax organization

I have been learning MySQL for a few weeks recently, and this blog is dedicated to sorting out the basic syntax of MySQL.

Let’s first look at the three most basic statements of MySQL. For example: I want to find the names of teachers whose salary is greater than 80,000 in the instructor table below

Introduction to MySQL syntax organization

Related free learning recommendations:mysql video tutorial

select name -- 这是最后筛选的元素,注意,在MySQL中一切结果都是以表的形式,哪怕这个表只有一个记录 from instructor -- from语句表示从哪个表中进行查询 where salary > 80000; -- where语句相当于选择语句,限定条件,找出所需的记录
Copy after login

Introduction to MySQL syntax organization
The query results are as shown! These three statements are the three most important ones in MySQL, and basically all queries are inseparable from these three statements. But if you want to satisfy complex queries, more statements must be supported.

distinct: De-duplicate the results

/*用法如下,假如我想在上表instructor中,找出所有系的名字,可以发现在dept_name中,有的系是出现了一次以上, 因此要对系的名字进行去重*/select distinct dept_name from instructor; -- 这里不需要限定条件,因此不用where语句
Copy after login

*: Indicates all the keys of the current table. The so-called keys are actually the row fields of the table, such as the ID of the instructor table. , name, dept_name, etc.

/*类似上一个例子,我想找出instructor表中salary大于80000的教师,并显示这些老师的所有信息*/select * from instructor where salary > 80000; -- 其实不加分号也行,分号表示执行到此结束,接下来的语句不执行
Copy after login

When we filter more than one condition, for example, if I want to find teachers whose salary is greater than 80,000, I also need to specify teachers in the computer department, that is, I To find out the teachers in the computer department whose salary is greater than 80,000, we need to use the and statement

select * from instructor where salary > 80000 and dept_name = 'Comp. Sci.';/*同样有and语句就有or语句,or表示或,即满足一个条件即可。比如我想找出工资小于60000或者大于80000的教师*/select * from instructor where salary > 80000 or salary 

Next, we start to query between multiple tables, which is what we will do next. difficulty. Let's first add the basic concept of keys. We have already said what a key is. Here we talk about the primary key, also called the primary key. The primary key represents the key that uniquely determines a certain record. For example, our student ID is the only way to identify us on campus. Even if someone in the school has the same name as me, I can separate our identities through our student ID. It can be seen that the name is not the primary key. When there is a duplicate name, the name cannot uniquely identify a student.

Introduction to MySQL syntax organization
This is the teachers table. The ID key represents the teacher’s ID, course_id represents the course id, and semester represents the semester in which the course is started. If I want to find out what courses the teacher teaches , and display the teacher's name and course_id.

/*这条语句可以实现,但是请问为什么可以实现呢?那是因为两个表都有共同的主键:ID,当然teaches不止这一个主 键,我们看键旁边有个key,都是主键。但是我们不用管其他键,只要关注ID键就可以了,因为这是两个表中共有的。这 里我还要特意提一下两个表查询,其实是一个表的一个记录去遍历另外一个表的记录,当找到某一条instruction的id等 于teaches的id,就将这条记录保存到结果表中*/ SELECT NAME,course_id where instructor.`ID` = teaches.`ID`;
Copy after login

That’s it, let’s talk about nature join: natural connection. The modification operation is very simple, that is, save the records with equal primary keys in the two tables. If the two tables have multiple identical keys, then you must ensure that each identical primary key is the same before saving.

/*上述例子完全可以用自然连接来查询*/ SELECT NAME,course_id FROM instructor NATURAL JOIN teaches;/*如果你想知道自然连接后的表长啥样,我满足你*/ SELECT * -- 显示结果表的所有键 FROM instructor NATURAL JOIN teaches;
Copy after login

Introduction to MySQL syntax organization
We can see that the columns of the table have been significantly increased. In fact, the keys of the two tables are integrated together. If you still don’t fully understand natural connections, let me give you another example. For example, we have a student table
[‘Xu Xiaoming, No. 1’, ‘Huang Xiaoshan, No. 2’], where the primary key is the student number. There is also a score table
[‘No. 1, Chinese: 87, Mathematics: 98’, ‘No. 2, Chinese: 94, Mathematics: 82’], where the student number is also the primary key of this table. When we want to print the student table, we only need to connect the two tables naturally. During the natural connection process, the same learned records will be integrated into one record, and finally become
['No. 1, Xu Xiaoming, Chinese: 87 , Mathematics: 98', 'No. 2, Huang Xiaoshan, Chinese: 94, Mathematics: 82']. In fact, natural connection is an optimized version of Descartes product. You can understand Descartes product by yourself.

We can query variables or functions through select

SELECT 'dd';SELECT 10*20;SELECT NOW(),UNIX_TIMESTAMP();
Copy after login

I actually want everyone to pay attention here. The key after the select statement will become the key name of the result. Know this The follow-up will be of great help to our name change operation. For example, in the example just now:
Introduction to MySQL syntax organization

concat(): This is a function that connects two keys. Its usage is similar to python’s printf

/*通过concat函数来连接那么和dept_name*/ SELECT NAME,CONCAT(NAME,' : ',dept_name)FROM instructor;
Copy after login

Introduction to MySQL syntax organization
Not only the key name, but also the records with the key will be connected together.

as: Rename the key or table

/*比如刚刚那个例子*/ SELECT NAME,CONCAT(NAME,' : ',dept_name) as 'name+dept'FROM instructor;/*或者给表改名*/ SELECT NAME FROM instructor as i WHERE i.salary > 70000; -- 注意改名后,要想引用该表的键,要加上引用符号:.
Copy after login

Introduction to MySQL syntax organization

讲了这么就查询,这里讲一下创建表:create table。其实这个命令一般用的很少,我更喜欢用鼠标点击来创建表,而不是敲代码来创建。

/*创建一个与student表一样结构的表,什么叫一样结构,就是ss_1表中键于student一样*/ CREATE TABLE ss_1 LIKE student;
Copy after login

刚刚例子中出现了like,其实like还可以用于字符匹配

/*like语句来进行字符匹配*/ SELECT dept_name FROM department WHERE building LIKE 'Watson%'; -- 这里用到%,类似于正则中的?,表示任意多个字符。这个查询是想找出building -- 中含有Watson的记录。
Copy after login

order by:对结果表中的键进行排序,默认是升序,即记录从上往下逐个递增

/*order by 按照某个属性进行排序*/ /*这个查询是想找出物理系的老师,并按工资进行排序*/ SELECT NAME , salary FROM instructor WHERE dept_name = 'Physics'ORDER BY salary;/*既然有升序,那就有降序*/ SELECT NAME , salary FROM instructor WHERE dept_name = 'Physics'ORDER BY salary DESC; -- DESC表示降序 /*我们还可以对多个键进行排序*/ SELECT * FROM instructor ORDER BY salary DESC , NAME ASC; -- 这里是先对工资进行降序排序,当工资一样时,按英文首字母的ASC码值升序排 -- 序
Copy after login

Introduction to MySQL syntax organization
有没有想过为什么order by要在where语句后面呢?因为order by语句时针对结果表的,where语句之后才有结果表,这也与我之前强调MySQL查询结果一切都是表!哪怕这个表只有一个键甚至一条记录!

between and:选择区间内的记录

/*区间范围,注意是闭区间,即[90000 , 100000]*/ SELECT NAME FROM instructor WHERE salary BETWEEN 90000 AND 1000000;
Copy after login

当我们对查询多个条件时,有时候可以通过键匹配

/*类似于python的字典,里面的元素逐个对应*/ SELECT NAME,course_id FROM instructor,teaches WHERE (instructor.`ID`,dept_name) = (teaches.`ID`,'Biology');
Copy after login

下一篇博客将重点介绍多个表之间的查询,这也是重中之重,难点之一!

更多相关免费学习推荐:mysql教程(视频)

The above is the detailed content of Introduction to MySQL syntax organization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!