将所有一对一关系表组合到一个表中(MYSQL)

时间:2018-04-20 04:15:24

标签: mysql

==解决==

现有表格

sub_category

-------------------------------------
pdcatid     | pdmcatid  | name
1           | 1         | Sub Category 1
2           | 1         | Sub Category 2
3           | 2         | Sub Category 3
4           | 2         | Sub Category 4
5           | 3         | Sub Category 5
6           | 3         | Sub Category 6
-------------------------------------

类别

-----------------------------------
pdmcatid    | pdlcatid  | name
1           | 1         | Category 1
2           | 2         | Category 2
3           | 2         | Category 3
-----------------------------------

main_category

----------------------------
pdlcatid    | name
1           | Main Category 1
2           | Main Category 2
----------------------------

根据3个表格中的关系,我坚持如何创建新的parent_ids

新表:类别(预期结果)

新的pdcatid序列并不重要,只要我能保持正确的关系(因此:parent_id)。

------------------------------------------------------
pdcatid     | parent_id     | old_pdcatid   | name
1           | 0             | null          | Main Category 1
2           | 0             | null          | Main Category 2
3           | 1             | null          | Category 1
4           | 2             | null          | Category 2
5           | 2             | null          | Category 3
6           | 3             | 1             | Sub Category 1
7           | 3             | 2             | Sub Category 2
8           | 4             | 3             | Sub Category 3
9           | 4             | 4             | Sub Category 4
10          | 5             | 5             | Sub Category 5
11          | 5             | 6             | Sub Category 6
------------------------------------------------------

2 个答案:

答案 0 :(得分:1)

一步一步地执行此操作:

create table categories
(
  pdcatid int not null auto_increment,
  parent_id int,
  pdcatid_old int,
  pdmcatid_old int,
  pdlcatid_old int,
  name varchar(200),
  primary key (pdcatid)
);

insert into categories(pdlcatid_old, name)
  select pdlcatid, name
  from main_category;

insert into categories(pdmcatid_old, pdlcatid_old, name, parent_id)
  select pdmcatid, pdlcatid, name,
    (
      select pdcatid
      from categories
      where categories.pdlcatid_old = category.pdlcatid
    )
  from category;

insert into categories(pdcatid_old, pdmcatid_old, name, parent_id)
  select pdcatid, pdmcatid, name,
    (
      select pdcatid
      from categories
      where categories.pdmcatid_old = sub_category.pdmcatid
    )
  from sub_category;

(当然,您可以使用一个联合所有插入和一个或两个后续更新来执行相同操作。)

然后,您可以删除您不想要的列,并在parent_id上添加非空约束和外键约束。

我宁愿顺便打电话给新的pdcatid id。 (或者称之为categories_id,但请致电parent_id parent_categories_id以保持一致。)

答案 1 :(得分:0)

我已经设法通过以下两个步骤完成上述步骤:

CREATE TABLE categories (id INT AUTO_INCREMENT PRIMARY KEY) AS
SELECT pdlcatid AS pdcatid, 0 AS parent_id, 0 AS old_parent_id, null AS old_pdcatid, name, 'pdlcat' AS cat FROM main_category
UNION ALL
SELECT pdmcatid AS pdcatid, 0 AS parent_id, pdlcatid AS old_parent_id, null AS old_pdcatid, name, 'pdmcat' AS cat FROM category
UNION ALL
SELECT pdcatid, 0 AS parent_id, pdmcatid AS old_parent_id, pdcatid AS old_pdcatid, name, 'pdcat' AS cat FROM sub_category

注意:当我创建新表时,我追加旧表“cat”的旧关系和别名

然后用旧关系更新新关系

UPDATE categories as t 
LEFT JOIN (SELECT t1.id, 
    CASE t1.cat 
    WHEN 'pdcat' THEN (SELECT id FROM categories as t2 WHERE t2.cat = 'pdmcat' AND t2.pdcatid = t1.old_parent_id) 
    WHEN 'pdmcat' THEN (SELECT id FROM categories as t2 WHERE t2.cat = 'pdlcat' AND pdcatid = t1.old_parent_id) 
    ELSE 0 END as new_parent_id FROM categories as t1) as t3 
ON t.id = t3.id 
SET t.parent_id = t3.new_parent_id

最后删除不需要的列

enter image description here