如何在引用自身时将 table 加入自身
How to join table to itself when it references to itself
我有 table,其中有两列引用自身。
select * from cgroups;
+----+--------------+-------------+-----------+----------+----------+
| id | title | description | cunixperm | cgroup_1 | cgroup_2 |
+----+--------------+-------------+-----------+----------+----------+
| 1 | admin | <null> | 32 | 1 | <null> |
| 2 | tag_mng | <null> | 32 | 1 | <null> |
| 3 | tags | <null> | 32 | 1 | <null> |
| 4 | exam_mng | <null> | 32 | 1 | <null> |
| 5 | exam_writer | <null> | 32 | 1 | <null> |
| 6 | exam_viewer | <null> | 32 | 1 | <null> |
| 7 | exam_starter | <null> | 32 | 1 | <null> |
+----+--------------+-------------+-----------+----------+----------+
这是它的代码:-
create table cgroups
(
id int unsigned primary key auto_increment,
title varchar(100) not null unique,
description varchar(255),
cunixperm tinyint unsigned not null default 32 ,# r=2 w=1
cgroup_1 int unsigned not null default 1 references cgroups (id) on delete cascade on update cascade,
cgroup_2 int unsigned references cgroups (id) on delete cascade on update cascade
);
我想创建视图,其中 cgroup_1 列替换为 c_group
的实际标题
像这样:- http://sqlfiddle.com/#!9/7af382/1
select c.id, c.title, c.description, c.cunixperm, c1.title cgroup_1 from cgroups c , cgroups c1
where c.cgroup_1 = c1.id;
+----+--------------+-------------+-----------+----------+
| id | title | description | cunixperm | cgroup_1 |
+----+--------------+-------------+-----------+----------+
| 1 | admin | <null> | 32 | admin |
| 2 | tag_mng | <null> | 32 | admin |
| 3 | tags | <null> | 32 | admin |
| 4 | exam_mng | <null> | 32 | admin |
| 5 | exam_writer | <null> | 32 | admin |
| 6 | exam_viewer | <null> | 32 | admin |
| 7 | exam_starter | <null> | 32 | admin |
+----+--------------+-------------+-----------+----------+
这种方法的问题是它只适用于 1 个参考列,而我有两个(其他 tables 中有 8 个)
如果我遵循,
select c.id, c.title, c.description, c.cunixperm, c1.title, c2.title
from cgroups c , cgroups c1, cgroups c2
where c.cgroup_1 = c1.id and c.cgroup_2 = c2.id;
使用您的 fiddle 似乎可行;无论如何你应该使用显式连接语法:
select c.id, c.title, c.description, c.cunixperm, c1.title, c.cgroup_1
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id;
http://sqlfiddle.com/#!9/7af382/3
和两个加入
select c.id, c.title, c.description, c.cunixperm, c1.title, c.cgroup_1 , c2.title cgroup_2
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id
INNER JOIN cgroups c2 ON c.cgroup_2 = c2.id
通过修改caisEdge,Answer让它工作:-
select c.id, c.title, c.description, c.cunixperm, c1.title cgroup_1 , c2.title cgroup_2
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id
INNER JOIN cgroups c2 ON c.cgroup_2 = c2.id;
我有 table,其中有两列引用自身。
select * from cgroups;
+----+--------------+-------------+-----------+----------+----------+
| id | title | description | cunixperm | cgroup_1 | cgroup_2 |
+----+--------------+-------------+-----------+----------+----------+
| 1 | admin | <null> | 32 | 1 | <null> |
| 2 | tag_mng | <null> | 32 | 1 | <null> |
| 3 | tags | <null> | 32 | 1 | <null> |
| 4 | exam_mng | <null> | 32 | 1 | <null> |
| 5 | exam_writer | <null> | 32 | 1 | <null> |
| 6 | exam_viewer | <null> | 32 | 1 | <null> |
| 7 | exam_starter | <null> | 32 | 1 | <null> |
+----+--------------+-------------+-----------+----------+----------+
这是它的代码:-
create table cgroups
(
id int unsigned primary key auto_increment,
title varchar(100) not null unique,
description varchar(255),
cunixperm tinyint unsigned not null default 32 ,# r=2 w=1
cgroup_1 int unsigned not null default 1 references cgroups (id) on delete cascade on update cascade,
cgroup_2 int unsigned references cgroups (id) on delete cascade on update cascade
);
我想创建视图,其中 cgroup_1 列替换为 c_group
的实际标题像这样:- http://sqlfiddle.com/#!9/7af382/1
select c.id, c.title, c.description, c.cunixperm, c1.title cgroup_1 from cgroups c , cgroups c1
where c.cgroup_1 = c1.id;
+----+--------------+-------------+-----------+----------+
| id | title | description | cunixperm | cgroup_1 |
+----+--------------+-------------+-----------+----------+
| 1 | admin | <null> | 32 | admin |
| 2 | tag_mng | <null> | 32 | admin |
| 3 | tags | <null> | 32 | admin |
| 4 | exam_mng | <null> | 32 | admin |
| 5 | exam_writer | <null> | 32 | admin |
| 6 | exam_viewer | <null> | 32 | admin |
| 7 | exam_starter | <null> | 32 | admin |
+----+--------------+-------------+-----------+----------+
这种方法的问题是它只适用于 1 个参考列,而我有两个(其他 tables 中有 8 个)
如果我遵循,
select c.id, c.title, c.description, c.cunixperm, c1.title, c2.title
from cgroups c , cgroups c1, cgroups c2
where c.cgroup_1 = c1.id and c.cgroup_2 = c2.id;
使用您的 fiddle 似乎可行;无论如何你应该使用显式连接语法:
select c.id, c.title, c.description, c.cunixperm, c1.title, c.cgroup_1
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id;
http://sqlfiddle.com/#!9/7af382/3
和两个加入
select c.id, c.title, c.description, c.cunixperm, c1.title, c.cgroup_1 , c2.title cgroup_2
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id
INNER JOIN cgroups c2 ON c.cgroup_2 = c2.id
通过修改caisEdge,Answer让它工作:-
select c.id, c.title, c.description, c.cunixperm, c1.title cgroup_1 , c2.title cgroup_2
from cgroups c
INNER JOIN cgroups c1 ON c.cgroup_1 = c1.id
INNER JOIN cgroups c2 ON c.cgroup_2 = c2.id;