我有三张桌子:
CREATE TABLE category (id integer, parent_id integer);
CREATE TABLE category_name (id integer, name (varchar(20), language_id);
CREATE TABLE language (language_id integer, language_name);
我可以通过以下语句获得子-父母-祖父母关系:
SELECT c.id, c.parent_id, p.parent_id
FROM category AS p, category AS c
WHERE p.id = c.parent_id AND p.parent_id IS NOT NULL;
但是我还需要从表 category_name 中获取孩子、父母和祖父母的名字,其中 language_id = 3
在没有子-父母-祖父母关系的情况下,获取名称的查询将是这样的:
SELECT c.id, n.name
FROM category c
LEFT JOIN category_name n ON c.id = n.id AND language_ID = 3
但是如何将这两个语句合并为一个,以便我实际上得到三个名称列呢?也就是说,每一行都应该有自己的 ID 和名称,它的父母和它的祖父母(如果后者存在的话)
如何继续进行此组合查询?
以下是表格和一些示例数据:
CREATE TABLE category (id integer, parent_id integer);
INSERT INTO category VALUES (1,null);
INSERT INTO category VALUES (2,null);
INSERT INTO category VALUES (3,1);
INSERT INTO category VALUES (4,2);
INSERT INTO category VALUES (5,2);
INSERT INTO category VALUES (6,4);
INSERT INTO category VALUES (7,2);
INSERT INTO category VALUES (8,7);
INSERT INTO category VALUES (9,7);
INSERT INTO category VALUES (10,7);
INSERT INTO category VALUES (11,3);
CREATE TABLE category_name (id integer, name varchar(20), language_id integer);
INSERT INTO category_name VALUES (1, 'CDs', 3);
INSERT INTO category_name VALUES (2, 'Books', 3);
INSERT INTO category_name VALUES (3, 'Music', 3);
INSERT INTO category_name VALUES (4, 'Novels', 3);
INSERT INTO category_name VALUES (5, 'Childrens books', 3);
INSERT INTO category_name VALUES (6, 'European', 3);
INSERT INTO category_name VALUES (7, 'Science', 3);
INSERT INTO category_name VALUES (8, 'Math', 3);
INSERT INTO category_name VALUES (9, 'Informatics', 3);
INSERT INTO category_name VALUES (10, 'Physics', 3);
INSERT INTO category_name VALUES (11, 'Classic', 3);
INSERT INTO category_name VALUES (1, 'CDs', 4);
INSERT INTO category_name VALUES (2, 'Livres', 4);
INSERT INTO category_name VALUES (3, 'Musique', 4);
INSERT INTO category_name VALUES (4, 'Romans', 4);
CREATE TABLE language (language_id integer, language_name varchar(15));
INSERT INTO language (3, 'English');
INSERT INTO language (4, 'French');
这是我想获取一些样本的方式(对于 c.id = 5 和 8):
c.id | c.parent_id | p.parent_id | nameChild | nameParent | nameGrandparent
5 | 2 | null | 'Children's books' | 'Books' | null
8 | 7 | 2 | 'Math' | 'Science' | 'Books'
我尝试了以下代码,但它给出了错误代码:1054。“on 子句”中的未知列“p.parent_id”
SELECT c.id, c.parent_id, p.parent_id, nc.name, np.name, ng.name
FROM category1 AS p, category1 AS c
LEFT JOIN category_name1 AS nc ON c.id = nc.id AND nc.language_id = 3
LEFT JOIN category_name1 AS np ON c.parent_id = np.id AND np.language_id = 3
LEFT JOIN category_name1 AS ng ON p.parent_id = ng.id AND ng.language_id = 3 AND p.parent_id IS NOT NULL
WHERE p.id = c.parent_id AND p.parent_id IS NOT NULL;
说出孩子和父母的名字我没有问题,但祖父母的名字似乎超出了我的掌握:(
下面是代码工作!
SELECT x.id, x.pid, x.gpid,
nc.name childName, np.name parentName, ng.name gpName
FROM
(SELECT c.id , c.parent_id AS pid, p.parent_id AS gpid
FROM
category AS c
LEFT JOIN category AS p ON p.id = c.parent_id
) x
LEFT JOIN category_name AS nc ON x.id = nc.id AND nc.language_id = 3
LEFT JOIN category_name AS np ON x.pid = np.id AND np.language_id = 3
LEFT JOIN category_name AS ng ON x.gpid = ng.id AND ng.language_id = 3;