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
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.
distinct: De-duplicate the results
/*用法如下,假如我想在上表instructor中,找出所有系的名字,可以发现在dept_name中,有的系是出现了一次以上, 因此要对系的名字进行去重*/select distinct dept_name from instructor; -- 这里不需要限定条件,因此不用where语句
*: 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; -- 其实不加分号也行,分号表示执行到此结束,接下来的语句不执行
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 <h2 id="span-style-color-rgb-font-size-px-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"><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="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/052/007b73b5402671007e13d9de7ee7de8d-2.png?x-oss-process=image/resize,p_40" class="lazy" 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.
We can query variables or functions through select
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(): 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;
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; -- 注意改名后,要想引用该表的键,要加上引用符号:.
讲了这么就查询,这里讲一下创建表:create table。其实这个命令一般用的很少,我更喜欢用鼠标点击来创建表,而不是敲代码来创建。
/*创建一个与student表一样结构的表,什么叫一样结构,就是ss_1表中键于student一样*/ CREATE TABLE ss_1 LIKE student;
刚刚例子中出现了like,其实like还可以用于字符匹配
/*like语句来进行字符匹配*/ SELECT dept_name FROM department WHERE building LIKE 'Watson%'; -- 这里用到%,类似于正则中的?,表示任意多个字符。这个查询是想找出building -- 中含有Watson的记录。
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码值升序排 -- 序
有没有想过为什么order by要在where语句后面呢?因为order by语句时针对结果表的,where语句之后才有结果表,这也与我之前强调MySQL查询结果一切都是表!哪怕这个表只有一个键甚至一条记录!
between and:选择区间内的记录
/*区间范围,注意是闭区间,即[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!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)