如何为查询合并两个表?

时间:2015-01-25 15:47:16

标签: sql postgresql

我有桌上动物:

CREATE TABLE animal
(
    id_animal serial primary key,
    id_parent INT,
    lft INT NOT NULL,
    rgt INT NOT NULL

);

表table_lang:

CREATE TABLE animal_lang
(
        id_animal_lang serial  primary key,
        id_animal int  constraint animal_lang_ibfk_1 references animal on delete cascade,
        code varchar(5) not null,
        name varchar(100) not null,
        descri varchar(255) not null
);

从表动物我使用查询获取数据:

SELECT animal.*, (COUNT(parent.id_animal) - 1) AS depth 
FROM animal AS animal, animal AS parent 
WHERE (animal.lft BETWEEN parent.lft AND parent.rgt) 
GROUP BY animal.id_animal;

如何使用表animal_lang中的'name'和'descri'来获取指定'code'的数据?

WHERE  animal_lang.code = 'eng'

结果应该看起来:

http://i.imgur.com/v8gvs75.png

我正在使用Nested Tree Model

动物的样本数据:

INSERT INTO animal VALUES (43, 1, 2, 5);
INSERT INTO animal VALUES (46, 44, 7, 8);
INSERT INTO animal VALUES (47, 43, 3, 4);
INSERT INTO animal VALUES (1, NULL, 1, 14);
INSERT INTO animal VALUES (44, 1, 6, 11);
INSERT INTO animal VALUES (45, 1, 12, 13);
INSERT INTO animal VALUES (48, 44, 9, 10);

animal_lang的样本数据

INSERT INTO animal_lang VALUES (1, 43, 'eng', 'EnglishTitleXXX', 'EnglishDXXX');
INSERT INTO animal_lang VALUES (2, 45, 'eng', 'EnglishTitleYYY', 'EnglishDYYY');
INSERT INTO animal_lang VALUES (3, 44, 'eng', 'EnglishTitleZZZ', 'EnglishDZZZ');
INSERT INTO animal_lang VALUES (4, 1, 'eng', 'EnglishTitleUUU', 'EnglishDUUU');
INSERT INTO animal_lang VALUES (5, 46, 'eng', 'EnglishTitleQQQ', 'EnglishDQQQ');
INSERT INTO animal_lang VALUES (6, 47, 'eng', 'EnglishTitleDDD', 'EnglishDDDD');
INSERT INTO animal_lang VALUES (7, 48, 'eng', 'EnglishTitleHHH', 'EnglishDHHH');
INSERT INTO animal_lang VALUES (8, 43, 'ger', 'GermanTitleXXX', 'GermanDXXX');
INSERT INTO animal_lang VALUES (9, 45, 'ger', 'GermanTitleYYY', 'GermanDYYY');
INSERT INTO animal_lang VALUES (10, 44, 'ger', 'GermanTitleZZZ', 'GermanDZZZ');
INSERT INTO animal_lang VALUES (11, 1, 'ger', 'GermanTitleUUU', 'GermanDUUU');
INSERT INTO animal_lang VALUES (12, 46, 'ger', 'GermanTitleQQQ', 'GermanDQQQ');
INSERT INTO animal_lang VALUES (13, 47, 'ger', 'GermanTitleDDD', 'GermanDDDD');
INSERT INTO animal_lang VALUES (14, 48, 'ger', 'GermanTitleHHH', 'GermanDHHH');

我试着这样做:

SELECT animal.*, trans.*, (COUNT(parent.id_animal) - 1) AS depth
FROM animal AS animal, animal AS parent
LEFT JOIN animal_trans as trans on animal.id_animal = trans.id_animal
WHERE (animal.lft BETWEEN parent.lft AND parent.rgt)
GROUP BY animal.id_animal;

但发生此错误:

ERROR:  invalid reference to FROM-clause entry for table "animal"
LINE 3: LEFT JOIN animal_trans as trans on animal.id_animal = ...
                                             ^
HINT:  There is an entry for table "animal", but it cannot be referenced from this part of the query.

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
A.id_animal,A.id_parent,A.lft,A.rgt,
(SELECT count(*) 
FROM animal A2 
WHERE A.lft BETWEEN A2.lft AND A2.rgt)-1
as depth, 
B.name, 
B.descri
FROM animal A, animal_lang B
WHERE A.id_animal = B.id_animal
AND B.code = 'eng'

看起来你的“结果应该看起来”的例子。

答案 1 :(得分:0)

WHERE (animal.lft BETWEEN animal.lft AND animal.rgt)

假设animal.rgt >= animal.lft,此子句始终为真。你几乎肯定在BETWEEN之前想要别的东西。