连接 - 名字和姓氏相同列(Oracle SQL)

时间:2016-02-14 01:23:01

标签: join count concatenation

对Oracle SQL完全陌生,可以说:

尝试在与“作者”相同的列中显示作者姓名以及该作者撰写的书籍数量(按作者名字按升序排列)。

SELECT AUTHOR.FNAME || ' ' || AUTHOR.LNAME AS "Author",
COUNT(BOOKAUTHOR.AUTHORID) AS "Books written"
FROM BOOKS
INNER JOIN BOOKAUTHOR ON BOOKS.ISBN = BOOKAUTHOR.ISBN
INNER JOIN AUTHOR ON BOOKAUTHOR.AuthorID = AUTHOR.AuthorID
ORDER BY AUTHOR.FNAME ASC

连接似乎不起作用,并且整体查询存在问题。

以下是相关的表格:

图书:ISBN,TITLE

作者:AUTHORID,LNAME,FNAME

BookAuthor :ISBN,AUTHORID

即使我尝试使用SELECT和FROM进行简单连接,也不会显示任何结果。

例如:

SELECT FNAME || ' ' || LNAME "AUTHOR NAME"
FROM AUTHOR

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT CONCAT(CONCAT(AUTHOR.FNAME, ' '),
      AUTHOR.LNAME) AS "AUTHOR" 
COUNT(BOOKAUTHOR.AUTHORID) AS "Books written"
FROM BOOKS
INNER JOIN BOOKAUTHOR ON BOOKS.ISBN = BOOKAUTHOR.ISBN
INNER JOIN AUTHOR ON BOOKAUTHOR.AuthorID = AUTHOR.AuthorID
ORDER BY AUTHOR.FNAME ASC