试图编写一个查询,显示作者及其出版物,甚至是没有出版物的作者

时间:2017-03-09 00:23:44

标签: mysql sql

    SELECT a.auth_name AS 'NAME', p.pub_title AS 'PUBLICATION'
    FROM publication p
    LEFT OUTER JOIN author_publication d
    ON p.pub_id = d.pub_id
    LEFT OUTER JOIN(
    SELECT a.auth_name 
    FROM author a,author_publication d
    WHERE a.auth_id = d.auth_id);
    GO

并获得错误         Msg 102,Level 15,State 1,Line 8         &#39 ;;'附近的语法不正确。

2 个答案:

答案 0 :(得分:0)

像这样改变:

 SELECT a.auth_name AS 'NAME',d.auth_id, p.pub_title AS 'PUBLICATION' FROM publication p
    LEFT OUTER JOIN
     author_publication d
    ON p.pub_id = d.pub_id
    LEFT OUTER JOIN
    author as a 
    on a.auth_id = d.auth_id

SELECT a.auth_name AS 'NAME', c.pub_title AS 'PUBLICATION' FROM
(select p.pub_title AS 'PUBLICATION' from
(select * from  publication) as p
left outer join
(select * from author_publication) as d
ON p.pub_id = d.pub_id)as c
left outer join
(SELECT a.auth_name FROM author a) as a 
on ON p.pub_id = c.pub_id

答案 1 :(得分:0)

在子查询的最后一个外部联接之后需要一个ON子句。但是,您可能根本不需要子查询。我猜你想要这个:

SELECT a.auth_name AS 'NAME', p.pub_title AS 'PUBLICATION'
FROM publication p
LEFT OUTER JOIN author_publication d
    ON p.pub_id = d.pub_id
LEFT OUTER JOIN author a 
    ON d.auth_id = a.auth_id

在不知道您的数据库的情况下很难确定