前端全棧之路——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

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 | 女裝   |
+----+--------+-----------+--------+
複製代碼
相關文章
相關標籤/搜索