Related free learning recommendations: mysql video tutorial
select name -- 这是最后筛选的元素,注意,在MySQL中一切结果都是以表的形式,哪怕这个表只有一个记录 from instructor -- from语句表示从哪个表中进行查询 where salary > 80000; -- where语句相当于选择语句,限定条件,找出所需的记录
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.
/*用法如下,假如我想在上表instructor中,找出所有系的名字,可以发现在dept_name中,有的系是出现了一次以上, 因此要对系的名字进行去重*/select distinct dept_name from instructor; -- 这里不需要限定条件,因此不用where语句
/*类似上一个例子,我想找出instructor表中salary大于80000的教师,并显示这些老师的所有信息*/select * from instructor where salary > 80000; -- 其实不加分号也行,分号表示执行到此结束,接下来的语句不执行
select * from instructor where salary > 80000 and dept_name = 'Comp. Sci.';/*同样有and语句就有or语句,or表示或,即满足一个条件即可。比如我想找出工资小于60000或者大于80000的教师*/select * from instructor where salary > 80000 or salary <h2><span style="color: rgb(255, 160, 122); font-size: 16px;"> 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. </span></h2><p><img src="https://img.php.cn/upload/article/000/000/052/007b73b5402671007e13d9de7ee7de8d-2.png" alt="Introduction to MySQL syntax organization"><br> 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. </p><pre class="brush:php;toolbar:false">/*这条语句可以实现,但是请问为什么可以实现呢?那是因为两个表都有共同的主键:ID,当然teaches不止这一个主 键,我们看键旁边有个key,都是主键。但是我们不用管其他键,只要关注ID键就可以了,因为这是两个表中共有的。这 里我还要特意提一下两个表查询,其实是一个表的一个记录去遍历另外一个表的记录,当找到某一条instruction的id等 于teaches的id,就将这条记录保存到结果表中*/ SELECT NAME,course_id where instructor.`ID` = teaches.`ID`;
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;
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.
SELECT 'dd';SELECT 10*20;SELECT NOW(),UNIX_TIMESTAMP();
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:
/*通过concat函数来连接那么和dept_name*/ SELECT NAME,CONCAT(NAME,' : ',dept_name)FROM instructor;
Not only the key name, but also the records with the key will be connected together.
/*比如刚刚那个例子*/ SELECT NAME,CONCAT(NAME,' : ',dept_name) as 'name+dept'FROM instructor;/*或者给表改名*/ SELECT NAME FROM instructor as i WHERE i.salary > 70000; -- 注意改名后,要想引用该表的键,要加上引用符号:.
/*创建一个与student表一样结构的表,什么叫一样结构,就是ss_1表中键于student一样*/ CREATE TABLE ss_1 LIKE student;
/*like语句来进行字符匹配*/ SELECT dept_name FROM department WHERE building LIKE 'Watson%'; -- 这里用到%,类似于正则中的?,表示任意多个字符。这个查询是想找出building -- 中含有Watson的记录。
/*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码值升序排 -- 序
有没有想过为什么order by要在where语句后面呢?因为order by语句时针对结果表的,where语句之后才有结果表,这也与我之前强调MySQL查询结果一切都是表!哪怕这个表只有一个键甚至一条记录!
/*区间范围,注意是闭区间,即[90000 , 100000]*/ SELECT NAME FROM instructor WHERE salary BETWEEN 90000 AND 1000000;
/*类似于python的字典,里面的元素逐个对应*/ SELECT NAME,course_id FROM instructor,teaches WHERE (instructor.`ID`,dept_name) = (teaches.`ID`,'Biology');
下一篇博客将重点介绍多个表之间的查询,这也是重中之重,难点之一!
更多相关免费学习推荐: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!