E
E
ex3xeng2015-11-01 16:20:16
MySQL
ex3xeng, 2015-11-01 16:20:16

How to merge multiple tables?

CREATE TABLE IF NOT EXISTS `prefix_categories` (
      `cat_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL,
      `slug` varchar(255) NOT NULL,
      `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
      `num_sort` int(11) unsigned DEFAULT NULL,
      PRIMARY KEY (`cat_id`),
      UNIQUE KEY `slug` (`slug`),
      UNIQUE KEY `num_sort` (`num_sort`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
    
    CREATE TABLE IF NOT EXISTS `prefix_link` (
      `link_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL,
      `slug` varchar(255) NOT NULL,
      `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
      `num_sort` int(11) unsigned DEFAULT NULL,
      PRIMARY KEY (`link_id`),
      UNIQUE KEY `slug` (`slug`,`num_sort`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
    
    CREATE TABLE IF NOT EXISTS `prefix_menu` (
      `menu_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `slug` varchar(255) NOT NULL,
      `name` varchar(255) NOT NULL,
      PRIMARY KEY (`menu_id`),
      UNIQUE KEY `slug` (`slug`,`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
    
    CREATE TABLE IF NOT EXISTS `prefix_pages` (
      `page_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL,
      `slug` varchar(255) NOT NULL,
      `parent_id` int(11) unsigned NOT NULL,
      `num_sort` int(11) unsigned DEFAULT NULL,
      PRIMARY KEY (`page_id`),
      UNIQUE KEY `slug` (`slug`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `prefix_menu` (
  `menu_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `slug` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`menu_id`),
  UNIQUE KEY `slug` (`slug`,`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `prefix_menu` (`menu_id`, `slug`, `name`) VALUES (1, 'main', 'главное');

Good day! Above resulted the DB created by me
Prompt how to implement the menu on the basis of these tables?
there are categories/pages/links, of course, they will also have fields added, but there will still be four standard ones, I would like not just to glue them together, but to display a hierarchy, for example:
category
--link
----category
--page
link
--category
-- --page
------link
----page
how to implement what we have planned?
To begin with, the database architecture and the query itself, can this be done with a single query?
Tell me the actual query to the database, ideally I would like to make a selection by prefix_menu.slug

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Red Rain, 2015-11-01
@ex3xeng

Combine like this:

SELECT 'pc' table_name, * FROM prefix_categories 
UNION ALL
SELECT 'pl' table_name, * FROM prefix_link 
UNION ALL
SELECT 'pm1' table_name, *, 0 parent_id, NULL num_sort FROM prefix_menu
UNION ALL
SELECT 'pp' table_name, * FROM prefix_pages 
UNION ALL
SELECT 'pm2' table_name, *, 0 parent_id, NULL num_sort FROM prefix_menu;
Then you can use this as a subquery, by which to create a tree. But this is far from the best option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question