Home Database Mysql Tutorial MySQL(基础篇)之单表查询

MySQL(基础篇)之单表查询

Jun 07, 2016 pm 03:03 PM
create mysql Base data Inquire structure

一:表的结构和数据 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;

 

三:总结

单表查询就到此结束,大家要多多练习,谢谢大家,下期为大家带来聚合函数的使用.MySQL(基础篇)之单表查询

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

See all articles