下面一段程式碼是建立對應資料庫的sql程式碼:
複製程式碼 程式碼如下:
///////////////
//////無限分類的資料庫設計及範例
////////////////
mysql >建立資料庫db_kind;
查詢正常,1 行受影響
mysql> use db_kind;
資料庫已變更
mysql> tb_kind(
. id int,
-> path varchar(200)
-> );
查詢正常,受影響0 行
mysql>; insert into tb_kind values(null,"新聞",0,0);查詢
into tb_kind values(null,"Video",0,0);
查詢正常,1行受影響
mysql>; insert into tb_kind values(null,"圖片",0,0);
正常,1行被查詢正常,1行被查詢正常,1影響
mysql>; insert into tb_kind values(null,"博客",0,0);
查詢正常,1行受影響
mysql> insert into tb_kind values(null,"體育新聞",11,"0-1 into tb_kind values(null,"體育新聞",11,"0-1 ");
查詢正常,1行受影響
mysql> insert into tb_kind values(null,"娛樂新聞",1,"0-1");
查詢正常,1行受影響
my>; insert into tbkind values(null,"財經新聞",1,"0-1");
查詢正常,1行受影響
mysql>; select * from db_kind;
錯誤1146:表'db_kind.db_kind'不存在從tb 選擇*
_kind;
+----+------------+-----+------+
|編號| pname | 姓名PID |路徑|
+----+----------+-----+-----+
| 1 | 新聞 | 0 | 0 |
| 2 | 視訊 |
| 2 | 視訊 | | 0 |
| 3 | 圖片 | 0 | 0 |
| 4 |部落格 | 0 | 0 6 | 娛樂新聞| 1 | 0-1 |
| 7 | 財經新聞| 1 | 0-1 |
+----+---------+-----+------+
集合中的7 行
mysql> insert into tb_kind values(null,"籃球新聞",5,"0-1-5");
查詢正常,1行受影響
mysql>; insert into tb_kind values(null,"足球新聞",5," 0-1-5");
查詢正常,1行受影響
mysql>;從tb_kind 中選擇*;
+----+---------+-----+- ------+
|編號| pname | 姓名PID |路徑|
+----+----------+-----+-------- +
| 1 | 新聞 | 0 | 0 |
| 2 | 影片 | 0 | 0 |
| 4 | 部落格 | 0 | 0 |
| 5 | 體育新聞| 1 | 0-1 |
| 6 | 娛樂新聞| 1 | 0-1 |
| 7 | 金融新聞| 1 | 0-1 新聞| 5 | 0-1-5 |
+----+----------+-----+--------+
集合中的9 行
mysql> insert into tb_kind values(null,"NBA",8,"0-1-5-8");
查詢正常,1行受影響
mysql>; insert into tb_kind values(null,"CBA",8 ,"0-1-5-8");
查詢正常,1行受影響
mysql>;從tb_kind 中選擇*;
+----+---------+--- --+---------+
|編號| pname | 姓名| PID |路徑 |
+----+---------+-----+-- -------+
| 1 | News | 0 | 0 |
| 2 | 視訊 | 0 | 0 0 |
| 4 | 部落格 | 0 | 0 |
| 5 | 體育新聞| 1 | 0-1 |
| 6 | 娛樂新聞| 1 | 0-1 |
| 0-1- 5 |
| 9 | 足球新聞| 5 | 0-1-5 |
| 10 | 10 NBA | 8 -5- 8 |
+----+---------+-----+---------+
集合中11 行
mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set
mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set
mysql> select concat(path,"-",id) as abs from tb_kind order by abs.path;
ERROR 1054 : Unknown column 'abs.path' in 'order clause'
mysql> select concat(path,"-",id) as abs from tb_kind order by abs
+------------+
| abs |
+------------+
| 0-1 |
| 0-1-5 |
| 0-1-5-8 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
| 0-1-5-9 |
| 0-1-6 |
| 0-1-7 |
| 0-2 |
| 0-3 |
| 0-4 |
+------------+
11 rows in set
mysql> select concat(path,"-",id) as,id,name,path abs from tb_kind order by abs;
ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id,name,path abs from tb_kind order by abs' at line 1
mysql> select concat(path,"-",id) as abs,
id,pname,path abs from tb_kind order by abs;
+------------+----+----------+---------+
| abs | id | pname | abs |
+------------+----+----------+---------+
| 0-1 | 1 | 新闻 | 0 |
| 0-1-5 | 5 | 体育新闻 | 0-1 |
| 0-1-5-8 | 8 | 篮球新闻 | 0-1-5 |
| 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |
| 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |
| 0-1-5-9 | 9 | 足球新闻 | 0-1-5 |
| 0-1-6 | 6 | 娱乐新闻 | 0-1 |
| 0-1-7 | 7 | 财经新闻 | 0-1 |
0-2 | 2 | 影片 | 0 |
| 0-3 | 4 | 部落格 | 0 |
+------------+ ----+----------+---------+
11 rows in set
mysql>
複製程式碼 代碼如下: