mysql的無限分類問題 MySQL無限分類

2021-10-22 09:55:27 字數 990 閱讀 6538

無限分類:不管什麼分類就放在分類表中

create table loop_category (

id int primary key auto_increment,

name varchar(20),

pid int

insert into loop_category (name, pid) values ('手機', null), ('電視', null), ('空調', null);

insert into loop_category (name, pid) values ('蘋果手機',1), ('華為手機',1), ('小公尺手機',1), ('魅族手機',1), ('錘子手機',1);

insert into loop_category (name, pid) values ('夏普電視',2), ('樂視電視',2), ('tcl電視',2), ('創維電視',2), ('海爾電視',2);

insert into loop_category (name, pid) values ('格力空調',3), ('海爾空調',3), ('美的空調',3), ('奧克斯空調',3), ('志高空調',3);

-- 查詢特殊的

-- 先查詢手機的id

select id from loop_category where name='手機'; -- 手機的id

-- 在根據pid等於手機的id

select * from loop_category where pid = (select id from loop_category where name='手機'); -- 手機的分類

-- 自連線

select c1.`id`, c1.`name` from loop_category c1 inner join loop_category c2 on c1.pid = c2.`id` where c2.`name`='電視';

標籤:category,name,分類,pid,無限,mysql,手機,id,loop

mysql部門無限分類 mysql 無限級分類

兩種思路吧,遞迴 和 非遞迴 遞迴 arr 1 id 1,pid 0 2 id 2,pid 0 3 id 3,pid 1 4 id 4,pid 1 5 id 5,pid 0 6 id 6,pid 3 7 id 7,pid 6 8 id 8,pid 3 9 id 9,pid 4 10 id 10,pi...

關於無限分類的呼叫問題!

資料表test是這麼建的,乙個id pid,name。方法一 呼叫此id下所有的子類。function tree pid return arr 此方法返回的是乙個多維陣列,有時候會需要轉化成字串。也提供了方法 cycle function cycle arr return idstr 向上取name...

mysql無限級分類表設計

create table tdb goods types type id smallint unsigned primary key auto increment,type name varchar 20 not null,parent id smallint unsigned not null d...