好的,我对此查询有困难,我想要做的是将titles.pub_id加入publishers.pub_id,然后将publishers.pub_id加入employee.pub_id。
这是我到目前为止所提出的
在titles表中
title_id
是主键
pub_id
是外键
在publishers表中
pub_id
是主键
pub_name
是一个canidate键
pub_id
中的是外键
SELECT titles.title_id
FROM titles
INNER JOIN titleauthor on titleauthor.title_id = titles.title_id
--INNER JOIN employee on employee.pub_id = publishers.pub_id
--INNER JOIN publishers on publishers.pub_id = titles.pub_id
我从此查询中获得的错误是
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "publishers.pub_id" could not be bound.
答案 0 :(得分:2)
联接是左关联的,因此ON
子句只能引用先前在查询中连接的表,而不能引用以后连接的表。所以你需要改变这两行的顺序:
INNER JOIN employee on employee.pub_id = publishers.pub_id
INNER JOIN publishers on publishers.pub_id = titles.pub_id
给出这个:
INNER JOIN publishers on publishers.pub_id = titles.pub_id
INNER JOIN employee on employee.pub_id = publishers.pub_id
以使on employee.pub_id = publishers.pub_id
在加入之前不引用publishers
。
答案 1 :(得分:0)
SELECT title_id
FROM (
(
titles LEFT JOIN titleauthor USING(title_id)
) LEFT JOIN publishers USING(pub_id)
) LEFT JOIN employee USING(pub_id)
注意:在我看来,你需要从这个查询中获得的东西可以通过使用LEFT JOIN而不是INNER JOIN来实现(查询更快,消耗更少的内存)