MySQL(基础篇)之单表查询
一:表的结构和数据 CREATE TABLE `t_student` ( `id` INT PRIMARY KEY , `stuName` VARCHAR (10) NOT NULL, `age` INT NOT NULL , `sex` VARCHAR (4) , `gradeName` VARCHAR (10) NOT NULL ); INSERT INTO `t_student` (`id`, `stuName`, `age`, `sex`, `gra
一:表的结构和数据
CREATE TABLE `t_student` (
`id` INT PRIMARY KEY ,
`stuName` VARCHAR (10) NOT NULL,
`age` INT NOT NULL ,
`sex` VARCHAR (4) ,
`gradeName` VARCHAR (10) NOT NULL
);
INSERT INTO `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) VALUES('1','张三',23,'男','一年级'),('2','张三丰',25,'男','二年级'),('3','李四',23,'男','一年级'),
('4','王五',22,'男','三年级'),('5','珍妮',21,'女','一年级'),('6','李娜',26,'女','二年级'),('7','王峰',20,'男','三年级'),('8','黄志浩',21,'男','二年级'),
('9','杨磊',22,'男','一年级'),('10','邹涛',25,'男','二年级'),('11','鲍文杰',21,NULL,'二年级'),('12','朱云芬',23,'男','二年级'),('13','朱云峰','24',NULL,'二年级');
这是我为大家练习准备的数据,下面正式开始单表查询.
二:单表查询
2.1 查询所有字段
1. Select 字段1,字段2,字段3……From 表名;
例:SELECT id,stuName,age,sex,gradeName FROM t_student;
2. Select * from 表名;
例:SELECT * FROM t_student;
两者区别: 前者可以通过改变查询字段的顺序来调整查询结果显示的顺序.
2.2 查询指定字段
Select 字段1,字段2,字段3……From 表名;
例:/*查询t_student表的所有age,sex,gradeName字段*/
SELECT age,sex,gradeName FROM t_student;
2.3 Where条件查询
1. Select 字段1,字段2,字段3……From 表名 Where 条件表达式;
例: 查询编号为1的学生
SELECT * FROM t_student WHERE id =1;
例: 查询年龄大于22的学生
SELECT * FROM t_student WHERE age>22;
例: 查询性别为男的学生
SELECT * FROM t_student WHERE sex='男';
2.4 带IN关键字查询
1. Select 字段1,字段2,字段3……From 表名 Where 条件表达式 [NOT] IN(元素1,元素2,元素3);
例: 查询年龄为22或者23的学生记录
SELECT * FROM t_student WHERE age IN (22,23);
例: 查询编号不为1和9的学生记录
SELECT * FROM t_student WHERE id NOT IN(1,9);
2.5 带BETWEEN的范围查询
1. Select 字段1,字段2,字段3……From 表名 Where 条件表达式 [NOT] BETWEEN 数值1 AND 数值2;
例: 查询编号为1-9的学生记录
SELECT * FROM t_student WHERE id BETWEEN 1 AND 9;
例: 查询年龄小于22大于25的学生记录
SELECT * FROM t_student WHERE age NOT BETWEEN 22 AND 25;
2.6 带Like的模糊查询
Select 字段1,字段2,字段3……From 表名 Where 条件表达式 [NOT] Like ‘字符串’;
‘%’: 代表任意字符,可以有或者没有
‘_’: 代表单个字符,必须要有
例: 查询姓名中包含张的学生记录
SELECT * FROM t_student WHERE stuName LIKE '%张%';
例: 查询姓名中第一个字为张的学生记录
SELECT * FROM t_student WHERE stuName LIKE '张%';
例: 查询姓名中第一个字为张,并且姓名只有2个字的学生记录
SELECT * FROM t_student WHERE stuName LIKE '张_';
2.7 空值查询
Select 字段1,字段2,字段3……From 表名 Where 条件表达式 IS[NOT] NULL;
例: 查询性别为null的学生记录
SELECT * FROM t_student WHERE sex IS NULL;
例: 查询性别不为null的学生记录
SELECT * FROM t_student WHERE sex IS NOT NULL;
2.8 带AND的多条件查询
Select 字段1,字段2,字段3……From 表名 Where 条件表达式1 AND 条件表达式2[...AND 条件表达式N];
例: 查询一年级中性别为男的学生记录
SELECT * FROM t_student WHERE gradeName='一年级' AND sex='男';
例: 查询二年级中年龄为22-24的学生记录
SELECT * FROM t_student WHERE gradeName='二年级' AND age BETWEEN 22 AND 24;
2.9 带OR的多条件查询
Select 字段1,字段2,字段3……From 表名 Where 条件表达式1 OR 条件表达式2[...OR 条件表达式N];
例: 查询年级为一年级或者年龄为23的学生记录
SELECT * FROM t_student WHERE gradeName='一年级' OR age=23;
2.10 DISTINCT去重复查询
Select Distinct 字段名 From 表名;
例子: 查询学生表所有的年级
SELECT DISTINCT gradeName FROM t_student ;
不加DISTINCT会有很多重复记录
2.11对查询结果进行排序
Select 字段1,字段2,字段3……From 表名ORDER BY 属性名 [ASC|DESC]
例: 查询所有学生记录并按年龄降序排序
SELECT * FROM t_student ORDER BY age DESC;
2.12 GROUP BY 分组查询
Select 字段1,字段2,字段3……From 表名 GROUP BY属性名;[HAVING 条件表达式][WITH ROLLUP];
1. 单独使用(毫无意义);
2. 与GROUP_CONCAT()函数一起使用;
3. 与聚合函数一起使用;
4. 与HAVING一起使用(对查询结果的筛选);
5. 与WITH ROLLUP一起使用(最后加入一个总和行);
例: 查询每个年级的所有学生姓名
SELECT gradeName,GROUP_CONCAT(stuName) FROM t_student GROUP BY gradeName;
例: 查询每个年级的学生个数
SELECT COUNT(stuName) AS '学生数量',gradeName AS '年级' FROM t_student GROUP BY gradeName;
例: 查询每个年级的学生个数并且选出学生数量大于3的年级
SELECT COUNT(stuName) AS '学生数量',gradeName AS '年级' FROM t_student GROUP BY gradeName HAVING COUNT(stuName)>3;
例: 查询每个年级的学生个数并在末尾加入一个总和行
SELECT COUNT(stuName) AS '学生数量',gradeName AS '年级' FROM t_student GROUP BY gradeName WITH ROLLUP;
2.13 LIMIT 分页查询
Select 字段1,字段2,字段3……From 表名 LIMIT 初始位置,记录数;
例: 查询所有学生记录的前5条记录
SELECT * FROM t_student LIMIT 0,5;
三:总结
单表查询就到此结束,大家要多多练习,谢谢大家,下期为大家带来聚合函数的使用.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

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.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

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.
