阅读 52

前端全栈之路——Mysql

centos安装mysql

$ sudo dnf install @mysql
$ sudo systemctl enable --now mysql
$ sudo systemctl status mysql
复制代码

连接数据库

mysql -u root -p 
复制代码

创建和删除数据库

  • 查看数据库
// 使用;结尾,表示这是一个完整的SQL语句
// SQL语句一般都是语意化的 show(显示) databases(数据库复数)
// 一开始显示的是mysql默认的数据库,里面存放了mysql服务的相关配置和信息
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
复制代码
  • 创建数据库
// 创建一个名称为studb的数据库 create(创建) database(数据库单数)
mysql> create database studb;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| studb              |
| sys                |
+--------------------+
复制代码
  • 删除数据库
// 删除名称为studb的数据库 drop(丢弃) 
// 警告:不要随意删除mysql默认的数据库,后果自负
mysql> drop database studb;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
复制代码

创建列表

  • 选择一个数据库
mysql> use studb;
复制代码
  • 创建列表
// 创建一个名称为student的table
// 一行输入显示不下可以enter换行
// SQL一句完整的语句是以;为标志的
// 一个table是由行和列组成的,列就是常说的表头
mysql> CREATE TABLE `student` (
    -> id INT,			//id列存储整数型数据
    -> name VARCHAR(64),	//name列存储可变字符型数据
    -> age INT,			//age列存储整数型数据
    -> city VARCHAR(64),	//city列存储可变字符型数据
    -> idcard VARCHAR(64)	//idcard列存储可变字符型数据
    -> );
    
// 显示studb下的所有table
mysql> show tables;
+-----------------+
| Tables_in_studb |
+-----------------+
| student         |
+-----------------+
复制代码
  • 显示列表列信息
// DESC(描述)
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(64) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| city   | varchar(64) | YES  |     | NULL    |       |
| idcard | varchar(64) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
复制代码
  • 删除列表
mysql> drop table student;
mysql> show tables;
Empty set (0.00 sec)
复制代码

列表行操作

  • 插入行数据
// 插入一行数据 INSERT(插入) INTO(往...里面) VALUES(值复数)
mysql> INSERT INTO student(id,name,age,city,idcard)
    -> VALUES(1,'张三',18,'北京','BJ123');
mysql> INSERT INTO student(id,name,age,city,idcard)
    -> VALUES(2,'李四',24,'上海','SH456');

// 显示筛选出符合条件的数据 SELECT(选择) FROM(从...中)
// * 代表所有的筛选条件,即显示所有的行信息
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 北京   | BJ123  |
|    2 | 李四    |   24 | 上海   | SH456  |
+------+--------+------+--------+--------+

注意事项:
1. 每次插入一行数据,并且是完整的数据,不能缺少字段
2. 每个数据的类型,数据精度和位数必须和对应的列一致
复制代码
  • 更新行数据
// 更新李四这一行的age和city数据
// 可以同时更新多列的数据用逗号隔开
// WHERE id=2 表示只更新id等于2的这一行 WHERE(在...哪些地方)
mysql> UPDATE student SET age=20,city='杭州' WHERE id=2;
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 北京   | BJ123  |
|    2 | 李四    |   20 | 杭州   | SH456  |
+------+--------+------+--------+--------+

// 不是用WHERE的区别
mysql> UPDATE student SET age=20,city='上海';
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   20 | 上海   | BJ123  |
|    2 | 李四    |   20 | 上海   | SH456  |
+------+--------+------+--------+--------+

// WHERE还可以混合其他运算符
mysql> UPDATE student SET age=18,city='杭州' WHERE city='上海' AND idcard= 'BJ123';
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 杭州   | BJ123  |
|    2 | 李四    |   20 | 上海   | SH456  |
+------+--------+------+--------+--------+
复制代码
  • 删除行数据
// 删除行信息 DELETE(删除)
// WHERE还可以混合其他运算符
// 如果存在主外键,必须县删除子表里面对应的行信息,再删除父表中的行信息
mysql> DELETE FROM student WHERE id=2;
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 杭州   | BJ123  |
+------+--------+------+--------+--------+
复制代码
  • 截断列表和清空列表
// 会重置标识符,下文解释
// 不写入日志,不能恢复
mysql> TRUNCATE table student;
Empty set (0.00 sec)

// 不会重置标识符,下文解释
// 会写入日志,能恢复
mysql> DELETE FROM student;
Empty set (0.00 sec)
复制代码

列表列操作

  • 删除列
// 删除idcard字段 ALTER(更改) 
mysql> ALTER TABLE student DROP idcard;
mysql> DESC student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(64) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
| city  | varchar(64) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
复制代码
  • 增加列
// 增加idcard字段 COLUMN(列)
mysql> ALTER TABLE student ADD COLUMN idcard VARCHAR(64);
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(64) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| city   | varchar(64) | YES  |     | NULL    |       |
| idcard | varchar(64) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
复制代码
  • 修改列的字段

// 修改idcard字段长度 MODIFY(修改)

mysql> ALTER TABLE student MODIFY idcard VARCHAR(128);
mysql> DESC student;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int          | YES  |     | NULL    |       |
| name   | varchar(64)  | YES  |     | NULL    |       |
| age    | int          | YES  |     | NULL    |       |
| city   | varchar(64)  | YES  |     | NULL    |       |
| idcard | varchar(128) | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+
复制代码

约束

  • 唯一索引约束
// 唯一约束就是该表中不允许这个字段存在重复的值
// 比如每个人的身份证号码是唯一的,不存在两个人是一样的身份证号码
// UNIQUE(唯一) INDEX(索引) uq_student_idcard(按照语意化取的名字)
mysql> ALTER TABLE student ADD UNIQUE INDEX uq_student_idcard(idcard);
mysql> DESC student;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int          | YES  |     | NULL    |       |
| name   | varchar(64)  | YES  |     | NULL    |       |
| age    | int          | YES  |     | NULL    |       |
| city   | varchar(64)  | YES  |     | NULL    |       |
| idcard | varchar(128) | YES  | UNI | NULL    |       |
+--------+--------------+------+-----+---------+-------+

// 当将某个字段增加唯一约束后,往表中增加数据出现重复的值,就会报错
mysql> INSERT INTO student (id,name,age,city,idcard)
    -> VALUE(1,'张三',18,'上海',123);
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 上海   | 123    |
+------+--------+------+--------+--------+
mysql>  INSERT INTO student (id,name,age,city,idcard)
    -> VALUE(2,'李四',24,'杭州',123);
ERROR 1062 (23000): Duplicate entry '123' for key 'student.uq_student_idcard'
复制代码
  • 默认约束
// 默认约束就是,往表中添加数据时,如果没有默认约束的字段赋值,那么就会使用默认值
mysql> ALTER TABLE student MODIFY city VARCHAR(64) DEFAULT '北京';
mysql> DESC student;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int          | YES  |     | NULL    |       |
| name   | varchar(64)  | YES  |     | NULL    |       |
| age    | int          | YES  |     | NULL    |       |
| city   | varchar(64)  | YES  |     | 北京     |       |
| idcard | varchar(128) | YES  | UNI | NULL    |       |
+--------+--------------+------+-----+---------+-------+

// 往表中增加数据时,没有给city赋值,使用的时默认值"北京"
mysql>  INSERT INTO student (id,name,age,idcard)
    -> VALUE(2,'李四',24,456);
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    1 | 张三    |   18 | 上海   | 123    |
|    2 | 李四    |   24 | 北京   | 456    |
+------+--------+------+--------+--------+
复制代码
  • 非空约束
// 非空约束,就是往表中增加数据的时候,非空约束的字段不能为空
// UNI + NOT NULL => PRI
  mysql> ALTER TABLE student MODIFY idcard VARCHAR(64) NOT NULL;
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(64) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| city   | varchar(64) | YES  |     | 北京     |       |
| idcard | varchar(64) | NO   | PRI | NULL    |       |
+--------+-------------+------+-----+---------+-------+

mysql> INSERT INTO student (id,name,age,idcard) VALUE(3,'王五',24,null);
ERROR 1048 (23000): Column 'idcard' cannot be null
复制代码
  • 主健约束
// UNI + NOT NULL => PRI
// 主键是被挑选出来,作为行的惟一标识关键字,即主键默认是唯一索引的,并且非空
// 去掉idcard的主键,只要去掉非空约束就可以了
mysql> ALTER TABLE student MODIFY idcard VARCHAR(64);
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | YES  |     | NULL    |       |
| name   | varchar(64) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| city   | varchar(64) | YES  |     | 北京     |       |
| idcard | varchar(64) | YES  | UNI | NULL    |       |
+--------+-------------+------+-----+---------+-------+

// 如果没有主键,那么表中的数据是按照插入的顺序来排列的,不便于查找
// 删除表中的数据
mysql> DELETE FROM student WHERE id=1 OR id= 2;
mysql> SELECT * FROM student;
Empty set (0.00 sec)
mysql> INSERT INTO student (id,name,age,idcard)
    -> VALUE(2,'李四',24,456);
mysql> INSERT INTO student (id,name,age,idcard)
    -> VALUE(1,'张三',18,123);
mysql> SELECT * FROM student;
+------+--------+------+--------+--------+
| id   | name   | age  | city   | idcard |
+------+--------+------+--------+--------+
|    2 | 李四    |   24 | 北京    | 456    |
|    1 | 张三    |   18 | 北京    | 123    |
+------+--------+------+--------+--------+

// 给id增加主键约束
// 数据就会按照正常的顺序来
mysql> ALTER TABLE student ADD PRIMARY KEY(id);
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   | PRI | NULL    |       |
| name   | varchar(64) | YES  |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
| city   | varchar(64) | YES  |     | 北京     |       |
| idcard | varchar(64) | YES  | UNI | NULL    |       |
+--------+-------------+------+-----+---------+-------+
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18  | 北京   | 123    |
|  2 | 李四   |   24  | 北京   | 456    |
+----+--------+------+--------+--------+

// 一般会给主键增加自动递增,这样的话就不用每次给id赋值,会自动递增
mysql> ALTER TABLE student MODIFY id INT AUTO_INCREMENT;
mysql> DESC student;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int         | NO   | PRI | NULL    | auto_increment |
| name   | varchar(64) | YES  |     | NULL    |                |
| age    | int         | YES  |     | NULL    |                |
| city   | varchar(64) | YES  |     | 北京     |                |
| idcard | varchar(64) | YES  | UNI | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
// 没有给id指定值
mysql> INSERT INTO student (name,age,idcard) VALUE('张三',18,789);
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18 | 北京    | 123    |
|  2 | 李四   |   24 | 北京    | 456    |
|  3 | 张三   |   18 | 北京    | 789    |
+----+--------+------+--------+--------+
复制代码
  • 外健
//创建另外一个成绩表score
mysql> CREATE TABLE score (
    -> student_id INT PRIMARY,
    -> grad INT NOT NULL
    -> )
    -> ;
mysql> show tables;
+-----------------+
| Tables_in_studb |
+-----------------+
| score           |
| student         |
+-----------------+ 
mysql> DESC score;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| id    | int  | NO   | PRI | NULL    |       |
| grade | int  | NO   |     | NULL    |       |
+-------+------+------+-----+---------+-------+

// 给score表增加外键fk_score_student_id将student和student表中的id进行关联
// CONSTRAINT(约束) FOREIGN KEY(外键) REFERENCES(参考物)
// 往score表中插入数据的时候,必须先在student表中先查一条对应的数据
// 这就是先有父,才有子
mysql> ALTER TABLE score ADD CONSTRAINT fk_score_student_id FOREIGN KEY(student_id) REFERENCES student(id);
mysql> INSERT INTO score (student_id,grade) VALUE(6,99);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails 
(`studb`.`score`, CONSTRAINT `fk_score_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`))
mysql> INSERT INTO score (student_id,grade) VALUE(1,99);
mysql> SELECT * FROM score;
+------------+-------+
| student_id | grade |
+------------+-------+
|          1 |    99 |
+------------+-------+

// 如果要删除一条student中的数据,必须先删除所有的依赖这条数据的数据
// 然后再删除这条数据,这就是先删除子表,再删除父表
mysql> DELETE FROM student WHERE id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`studb`.`score`, CONSTRAINT `fk_score_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`))
mysql> DELETE FROM score WHERE student_id=1;
mysql> DELETE FROM student WHERE id=1;
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  2 | 李四   |   24 | 北京    | 456    |
|  3 | 张三   |   18 | 北京    | 789    |
+----+--------+------+--------+--------+
复制代码

数据查询

  • 查询语法
// []表示可缺省
// ORDER BY(按照什么方式排序) ASC(升序) DESC(降序)
SELECT	  <列名>
FORM	  <表名>
[WHERE]	  <查询条件表达式>
[ORDER BY <排序的列名>[ASC或者DESC]]

mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18 | 北京    | 123    |
|  2 | 李四   |   24 | 上海    | 456    |
|  3 | 张三   |   18 | 杭州    | 789    |
|  4 | 赵六   |   26 | 北京    | 246    |
+----+--------+------+--------+--------+

// 多个排序条件可以使用逗号隔开
mysql> SELECT id,name
    -> FROM student
    -> WHERE city='北京'
    -> ORDER BY id DESC;
+----+--------+
| id | name   |
+----+--------+
|  4 | 赵六   |
|  1 | 张三   |
+----+--------+
复制代码
  • 别名
// as可以省略
mysql> SELECT id,name,city as home
    -> FROM student
    -> WHERE city='北京'
    -> ORDER BY id DESC;
+----+--------+--------+
| id | name   | home   |
+----+--------+--------+
|  4 | 赵六    | 北京   |
|  1 | 张三    | 北京   |
+----+--------+--------+
复制代码
  • 常量列
// as可以省略
mysql> SELECT id,name,city home,'中国' as country
    -> FROM student
    -> WHERE city='北京'
    -> ORDER BY id DESC;
+----+--------+--------+---------+
| id | name   | home   | country |
+----+--------+--------+---------+
|  4 | 赵六    | 北京   | 中国     |
|  1 | 张三    | 北京   | 中国     |
+----+--------+--------+---------+
复制代码
  • 分页
// 3,3 第一个3表示从3开始取(index),第二个3表示一个取几条(page_size)
mysql> SELECT id,name,city
    -> FROM student
    -> limit 3,3;
+----+--------+--------+
| id | name   | city   |
+----+--------+--------+
|  4 | 赵六    | 北京   |
+----+--------+--------+
复制代码

表连接

  • 内连接
// 用于连表查询,取交集
// 有两种写法 INNER JOIN(内连接)
// SELECT * FROM student,score WHERE student.id = score.student_id;
// SELECT * FROM student INNER JOIN score ON student.id = score.student_id;
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18  | 北京   | 123    |
|  2 | 李四   |   24  | 上海   | 456    |
|  3 | 张三   |   18  | 杭州   | 789    |
|  4 | 赵六   |   26  | 北京   | 246    |
+----+--------+------+--------+--------+
mysql> SELECT * FROM score;
+------------+-------+
| student_id | grade |
+------------+-------+
|          1 |    99 |
+------------+-------+
mysql> SELECT * FROM student,score WHERE student.id = score.student_id;
+----+--------+------+--------+--------+------------+-------+
| id | name   | age  | city   | idcard | student_id | grade |
+----+--------+------+--------+--------+------------+-------+
|  1 | 张三    |   18 | 北京   | 123    |          1 |    99 |
+----+--------+------+--------+--------+------------+-------+
复制代码
  • 左外连接
// 用于连表查询,取左全集加上交集,数据缺省补NULL
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18  | 北京   | 123    |
|  2 | 李四   |   24  | 上海   | 456    |
|  3 | 张三   |   18  | 杭州   | 789    |
|  4 | 赵六   |   26  | 北京   | 246    |
+----+--------+------+--------+--------+
mysql> SELECT * FROM score;
+------------+-------+
| student_id | grade |
+------------+-------+
|          1 |    99 |
+------------+-------+
mysql> SELECT * FROM student LEFT JOIN score ON student.id = score.student_id;
+----+--------+------+--------+--------+------------+-------+
| id | name   | age  | city   | idcard | student_id | grade |
+----+--------+------+--------+--------+------------+-------+
|  1 | 张三   |   18 | 北京    | 123    |          1 |    99 |
|  2 | 李四   |   24 | 北京    | 456    |       NULL |  NULL |
|  3 | 张三   |   18 | 北京    | 789    |       NULL |  NULL |
|  4 | 赵六   |   26 | 北京    | 246    |       NULL |  NULL |
+----+--------+------+--------+--------+------------+-------+
复制代码
  • 右外连接
// 用于连表查询,取右全集加上交集,数据缺省补NULL
mysql> SELECT * FROM student;
+----+--------+------+--------+--------+
| id | name   | age  | city   | idcard |
+----+--------+------+--------+--------+
|  1 | 张三   |   18  | 北京   | 123    |
|  2 | 李四   |   24  | 上海   | 456    |
|  3 | 张三   |   18  | 杭州   | 789    |
|  4 | 赵六   |   26  | 北京   | 246    |
+----+--------+------+--------+--------+
mysql> SELECT * FROM score;
+------------+-------+
| student_id | grade |
+------------+-------+
|          1 |    99 |
+------------+-------+
mysql> SELECT * FROM student RIGHT JOIN score ON student.id = score.student_id;
mysql> SELECT * FROM student RIGHT JOIN score ON student.id = score.student_id;
+------+--------+------+--------+--------+------------+-------+
| id   | name   | age  | city   | idcard | student_id | grade |
+------+--------+------+--------+--------+------------+-------+
|    1 | 张三    |   18 | 北京   | 123    |          1 |    99 |
+------+--------+------+--------+--------+------------+-------+
复制代码
  • 自连接
// 一般用于存储树形结构数据的表结构
mysql> CREATE table category(
    -> id INT PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(64) NOT NULL,
    -> parent_id INT 
    -> );
mysql> DESC category;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int         | NO   | PRI | NULL    | auto_increment |
| name      | varchar(64) | NO   |     | NULL    |                |
| parent_id | int         | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
mysql> INSERT INTO category(name,parent_id) VALUE('男装',0);
mysql> INSERT INTO category(name,parent_id) VALUE('女装',0);
mysql> INSERT INTO category(name,parent_id) VALUE('领带',1);
mysql> INSERT INTO category(name,parent_id) VALUE('裙子',2);
mysql> SELECT * FROM category;
+----+--------+-----------+
| id | name   | parent_id |
+----+--------+-----------+
|  1 | 男装   |         0 |
|  2 | 女装   |         0 |
|  3 | 领带   |         1 |
|  4 | 裙子   |         2 |
+----+--------+-----------+
mysql> SELECT c1.id,c1.name,c2.id parent_id,c2.name FROM 
    -> category c1 INNER JOIN category c2 ON
    -> c1.parent_id = c2.id;
+----+--------+-----------+--------+
| id | name   | parent_id | name   |
+----+--------+-----------+--------+
|  3 | 领带    |         1 | 男装   |
|  4 | 裙子    |         2 | 女装   |
+----+--------+-----------+--------+
复制代码
文章分类
前端
文章标签