# 插入数据 mysql> insert into student_info( number, name, sex, id_number, department, major, enrollment_time ) values ( 20210101, '狗哥', '男', '158177200301044792', '计算机学院', '计算机科学与工程', '2021-09-01' ), ( 20210102, '猫爷', '男', '151008200201178529', '计算机学院', '计算机科学与工程', '2021-09-01' ), ( 20210103, '艾希', '女', '171563200201178529', '计算机学院', '软件工程', '2021-09-01' ), ( 20210104, '亚索', '男', '141992200201078600', '计算机学院', '软件工程', '2021-09-01' ) , ( 20210105, '莫甘娜', '女', '181048200008156368', '航天学院', '飞行器设计', '2021-09-01' ), ( 20210106, '赵信', '男', '197995200201078445', '航天学院', '电子信息', '2021-09-01' ); Query OK, 5 rows affected ( 0. 01 sec ) mysql> select * from student_info; +----------+-----------+------+--------------------+-----------------+--------------------------+-----------------+ | number | name | sex | id_number | department | major | enrollment_time | +----------+-----------+------+--------------------+-----------------+--------------------------+-----------------+ | 20210101 | 狗哥 | 男 | 158177200301044792 | 计算机学院 | 计算机科学与工程 | 2021-09-01 | | 20210102 | 猫爷 | 男 | 151008200201178529 | 计算机学院 | 计算机科学与工程 | 2021-09-01 | | 20210103 | 艾希 | 女 | 171563200201178529 | 计算机学院 | 软件工程 | 2021-09-01 | | 20210104 | 亚索 | 男 | 141992200201078600 | 计算机学院 | 软件工程 | 2021-09-01 | | 20210105 | 莫甘娜 | 女 | 181048200008156368 | 航天学院 | 飞行器设计 | 2021-09-01 | | 20210106 | 赵信 | 男 | 197995200201078445 | 航天学院 | 电子信息 | 2021-09-01 | +----------+-----------+------+--------------------+-----------------+--------------------------+-----------------+ 6 rows in set ( 0. 00 sec )
Records: 5 Duplicates: 0 Warnings: 0 mysql> insert into student_score ( number, subject, score ) values ( 20210101, '计算机是怎样运行的', 78 ), ( 20210101, 'MySQL是怎样运行的', 88 ), ( 20210102, '计算机是怎样运行的', 100 ), ( 20210102, 'MySQL是怎样运行 的', 98 ), ( 20210103, '计算机是怎样运行的', 59 ), ( 20210103, 'MySQL是怎样运行的', 61 ), ( 20210104, '计算机是怎样运行的', 55 ), ( 20210104, 'MySQL是怎样运行的', 46 ); Query OK, 8 rows affected ( 0. 00 sec ) Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from student_score; +----------+-----------------------------+-------+ | number | subject | score | +----------+-----------------------------+-------+ | 20210101 | MySQL是怎样运行的 | 88 | | 20210101 | 计算机是怎样运行的 | 78 | | 20210102 | MySQL是怎样运行的 | 98 | | 20210102 | 计算机是怎样运行的 | 100 | | 20210103 | MySQL是怎样运行的 | 61 | | 20210103 | 计算机是怎样运行的 | 59 | | 20210104 | MySQL是怎样运行的 | 46 | | 20210104 | 计算机是怎样运行的 | 55 | +----------+-----------------------------+-------+ 8 rows in set ( 0. 00 sec )
# 查询单个列 mysql> select number from student_info; +----------+ | number | +----------+ | 20210104 | | 20210102 | | 20210101 | | 20210103 | | 20210105 | | 20210106 | +----------+ 6 rows in set ( 0. 00 sec )
# 别名 mysql> select number AS 学号 FROM student_info; +----------+ | 学号 | +----------+ | 20210104 | | 20210102 | | 20210101 | | 20210103 | | 20210105 | | 20210106 | +----------+ 6 rows in set ( 0. 00 sec )
# 查询多个列 mysql> select number, name, id_number, major from student_info; +----------+-----------+--------------------+--------------------------+ | number | name | id_number | major | +----------+-----------+--------------------+--------------------------+ | 20210101 | 狗哥 | 158177200301044792 | 计算机科学与工程 | | 20210102 | 猫爷 | 151008200201178529 | 计算机科学与工程 | | 20210103 | 艾希 | 171563200201178529 | 软件工程 | | 20210104 | 亚索 | 141992200201078600 | 软件工程 | | 20210105 | 莫甘娜 | 181048200008156368 | 飞行器设计 | | 20210106 | 赵信 | 197995200201078445 | 电子信息 | +----------+-----------+--------------------+--------------------------+ 6 rows in set ( 0. 00 sec )
mysql> select number AS 学号, name AS 姓名, id_number AS 身份证号, major AS 专业 from student_info; +----------+-----------+--------------------+--------------------------+ | 学号 | 姓名 | 身份证号 | 专业 | +----------+-----------+--------------------+--------------------------+ | 20210101 | 狗哥 | 158177200301044792 | 计算机科学与工程 | | 20210102 | 猫爷 | 151008200201178529 | 计算机科学与工程 | | 20210103 | 艾希 | 171563200201178529 | 软件工程 | | 20210104 | 亚索 | 141992200201078600 | 软件工程 | | 20210105 | 莫甘娜 | 181048200008156368 | 飞行器设计 | | 20210106 | 赵信 | 197995200201078445 | 电子信息 | +----------+-----------+--------------------+--------------------------+ 6 rows in set ( 0. 00 sec )
# 去重单列 mysql> select distinct department from student_info; +-----------------+ | department | +-----------------+ | 计算机学院 | | 航天学院 | +-----------------+ 2 rows in set ( 0. 00 sec )
# 多列去重 mysql> select distinct department, major from student_info; +-----------------+--------------------------+ | department | major | +-----------------+--------------------------+ | 计算机学院 | 计算机科学与工程 | | 计算机学院 | 软件工程 | | 航天学院 | 飞行器设计 | | 航天学院 | 电子信息 | +-----------------+--------------------------+ 4 rows in set ( 0. 00 sec )
# 按找从小到大排序 mysql> select * from student_score order by score; +----------+-----------------------------+-------+ | number | subject | score | +----------+-----------------------------+-------+ | 20210104 | MySQL是怎样运行的 | 46 | | 20210104 | 计算机是怎样运行的 | 55 | | 20210103 | 计算机是怎样运行的 | 59 | | 20210103 | MySQL是怎样运行的 | 61 | | 20210101 | 计算机是怎样运行的 | 78 | | 20210101 | MySQL是怎样运行的 | 88 | | 20210102 | MySQL是怎样运行的 | 98 | | 20210102 | 计算机是怎样运行的 | 100 | +----------+-----------------------------+-------+ 8 rows in set ( 0. 00 sec ) # 按照从大到小排序 mysql> select * from student_score order by score desc; +----------+-----------------------------+-------+ | number | subject | score | +----------+-----------------------------+-------+ | 20210102 | 计算机是怎样运行的 | 100 | | 20210102 | MySQL是怎样运行的 | 98 | | 20210101 | MySQL是怎样运行的 | 88 | | 20210101 | 计算机是怎样运行的 | 78 | | 20210103 | MySQL是怎样运行的 | 61 | | 20210103 | 计算机是怎样运行的 | 59 | | 20210104 | 计算机是怎样运行的 | 55 | | 20210104 | MySQL是怎样运行的 | 46 | +----------+-----------------------------+-------+ 8 rows in set ( 0. 00 sec )
# 按照多个列的值进行排序 mysql> select * from student_score order by subject, score DESC; +----------+-----------------------------+-------+ | number | subject | score | +----------+-----------------------------+-------+ | 20210102 | MySQL是怎样运行的 | 98 | | 20210101 | MySQL是怎样运行的 | 88 | | 20210103 | MySQL是怎样运行的 | 61 | | 20210104 | MySQL是怎样运行的 | 46 | | 20210102 | 计算机是怎样运行的 | 100 | | 20210101 | 计算机是怎样运行的 | 78 | | 20210103 | 计算机是怎样运行的 | 59 | | 20210104 | 计算机是怎样运行的 | 55 | +----------+-----------------------------+-------+ 8 rows in set ( 0. 01 sec )
# 限制显示条数 # 使用world数据库 mysql> use world; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> show tables; +-----------------+ | Tables_in_world | +-----------------+ | city | | country | | countrylanguage | +-----------------+ 3 rows in set ( 0. 00 sec )
mysql> describe city; +-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+----------------+ | ID | int | NO | PRI | NULL | auto_increment | | Name | char( 35 ) | NO | | | | | CountryCode | char( 3 ) | NO | MUL | | | | District | char( 20 ) | NO | | | | | Population | int | NO | | 0 | | +-------------+----------+------+-----+---------+----------------+
mysql> select ID, Name from city limit 9; +----+----------------+ | ID | Name | +----+----------------+ | 1 | Kabul | | 2 | Qandahar | | 3 | Herat | | 4 | Mazar-e-Sharif | | 5 | Amsterdam | | 6 | Rotterdam | | 7 | Haag | | 8 | Utrecht | | 9 | Eindhoven | +----+----------------+ 9 rows in set ( 0. 00 sec )
mysql> select ID, Name from city limit 9, 9; +----+-----------+ | ID | Name | +----+-----------+ | 10 | Tilburg | | 11 | Groningen | | 12 | Breda | | 13 | Apeldoorn | | 14 | Nijmegen | | 15 | Enschede | | 16 | Haarlem | | 17 | Almere | | 18 | Arnhem | +----+-----------+
|