nested - MySQL in clause to Enumeration Path on Table Heirachy -
i had investigation using mysql hierarchical data, (currently table uses adjacency list model) getting breadcrumb via loop sending parent id , re-cursing same function in php, fires query each time. want improve creating _construct method fire query once , return object can refer to.
to have done research nested sets, path enumeration (materialized path) , closure table.
i have chosen materialised path.
my sql
select t1.* menu t1 t1.menu_id in ( select trim(replace(t2.lineage,'/',',')) cat_idd menu t2 t2.page_id = 52 )
my table such, i'll paste in dump can test it...
structure
create table if not exists `menu` ( `menu_id` int(11) unsigned not null auto_increment, `menu_text` varchar(255) not null default 'new !!!', `menu_alt_text` varchar(255) default null, `menu_alt_location` varchar(255) default null, `page_id` int(11) unsigned not null default '0', `parent_menu_id` int(11) unsigned not null default '0', `lineage` varchar(255) default null, `menu_display_order` int(11) unsigned not null default '1', `menu_display` tinyint(1) unsigned not null default '1', `site_short_code` varchar(20) not null default 'default', `menu_can_delete` tinyint(1) not null default '1', `user_id` int(11) unsigned not null default '0', `opens_in_new_window` tinyint(1) unsigned not null default '0', primary key (`menu_id`), key `site_short_codee` (`site_short_code`) ) engine=innodb default charset=latin1 comment='this displays menu/navigation of site' auto_increment=75 ;
insert data
insert `menu` (`menu_id`, `menu_text`, `menu_alt_text`, `menu_alt_location`, `page_id`, `parent_menu_id`, `lineage`, `menu_display_order`, `menu_display`, `site_short_code`, `menu_can_delete`, `user_id`, `opens_in_new_window`) values (1, 'home', 'opens in same window', 'none', 1, 0, '0', 1, 1, 'site1', 0, 0, 0), (41, 'about us', 'about us, opens in same window', 'none', 42, 0, '0', 4, 1, 'site1', 1, 43, 0), (42, 'menu 3', 'menu 3, opens in same window', 'none', 43, 0, '0', 30, 1, 'site1', 1, 43, 0), (43, 'menu 4', 'menu 4 templates, opens in same window', 'none', 44, 42, '1/42', 9, 1, 'site1', 1, 43, 0), (44, 'menu 5', 'menu 5, opens in same window', 'none', 45, 42, '1/42', 3, 1, 'site1', 1, 43, 0), (45, 'menn 6', 'menu 6, opens in same window', 'none', 46, 42, '1/42', 6, 1, 'site1', 1, 43, 0), (46, 'menu 7', 'menu 7, opens in same window', 'none', 47, 0, '0', 43, 1, 'site1', 1, 43, 0), (47, 'menu 8', 'menu 8, opens in same window', 'none', 48, 46, '1/46', 3, 1, 'site1', 1, 43, 0), (48, 'menu 9', 'menu 9, opens in same window', 'none', 50, 46, '1/46', 6, 1, 'site1', 1, 43, 0), (49, 'menu 10', 'menu 10, opens in same window', 'none', 53, 48, '1/46/48', 9, 1, 'site1', 1, 43, 0), (50, 'menu 11', 'menu 11, opens in same window', 'none', 49, 46, '1/46', 12, 1, 'site1', 1, 43, 0), (51, 'menu 12', 'menu 12, opens in same window', 'none', 51, 48, '1/46/48', 15, 1, 'site1', 1, 43, 0), (52, 'menu 13', 'menu 13, opens in same window', 'none', 52, 48, '1/46/48', 18, 1, 'site1', 1, 43, 0), (53, 'menu 14, 'menu 14, opens in same window', 'none', 70, 0, '0', 7, 1, 'site1', 1, 43, 0), (54, 'release dates', 'release dates, opens in same window', 'none', 56, 53, '1/53', 12, 1, 'site1', 1, 43, 0), (55, 'clients', 'clients, opens in same window', 'none', 57, 0, '0', 65, 1, 'site1', 1, 43, 0), (56, 'menu 145 clients', 'menu 145 clients, opens in same window', 'none', 74, 55, '1/55', 3, 1, 'site1', 1, 43, 0), (57, 'ffff', 'ffff, opens in same window', 'none', 59, 55, '1/55', 6, 1, 'site1', 1, 43, 0);
this return list of menu_ids in linage
select trim(replace(t2.lineage,'/',',')) cat_idd menu t2 t2.page_id = 52
however when used subquery above returns 1 row. need return 3. i've tried using nest join too...
hmmm ideas appreciated.
thanks
andi
------ edit answering own question
select t1.* menu t1, menu t2 t2.page_id = 52 , find_in_set ( t1.menu_id, t2.lineage )
this seems work me... if helps else hope so. changed delimiter comma
thanks
ok think works find find history...
select t1.* menu t1, menu t2 t2.page_id = 52 , find_in_set ( t1.menu_id, t2.lineage )
if helps else hope so. changed delimiter comma in data in lineage field...
thanks