SQL子查询:需要返回两个值

时间:2017-09-06 18:27:28

标签: sql derby

我正在尝试将这两个查询组合在一起,但遇到了麻烦,因为子查询只能返回一个值。

我正在使用Netbeans IDE,Java DB和Apache Derby。

我需要归还的是作者的姓名,以及他们写的书籍数量 - 而且只有写得最多的人:

SELECT AUTHOR_FIRST_NAME || ' ' || AUTHOR_LAST_NAME AS AUTHOR_NAME
FROM Authors
WHERE AUTHOR_ID = (SELECT AUTHOR_ID, COUNT(*) AS num_books_read
FROM Books
GROUP BY AUTHOR_ID
ORDER BY num_books_read DESC
FETCH FIRST 1 ROW ONLY);

我也尝试过加入,但无法得到我需要的结果。

3 个答案:

答案 0 :(得分:0)

您不需要返回两个值:

positionLat = position.coords.latitude;

编辑:

如果需要实际数字,请将子查询移到SELECT AUTHOR_FIRST_NAME || ' ' || AUTHOR_LAST_NAME AS AUTHOR_NAME FROM Authors WHERE AUTHOR_ID = (SELECT AUTHOR_ID FROM Books GROUP BY AUTHOR_ID ORDER BY COUNT(*) DESC FETCH FIRST 1 ROW ONLY ); 子句:

FROM

答案 1 :(得分:0)

如何选择Author_Id,其中包含在子选择中编写的书籍数量,并使用作者表格执行INNER JOIN

SELECT A.AUTHOR_FIRST_NAME || ' ' || A.AUTHOR_LAST_NAME AS AUTHOR_NAME  
      ,MAX.CTR
 FROM Authors A
INNER JOIN 
     (
     SELECT AUTHOR_ID
           ,COUNT(*) AS CTR
       FROM Books
   GROUP BY AUTHOR_ID
   ORDER BY COUNT(*) DESC
      FETCH FIRST 1 ROW ONLY
     ) MAX
   ON MAX.AUTHOR_ID = A.AUTHOR_ID

答案 2 :(得分:0)

    select 
       A.author_first_name || ' ' || A.author_last_name as author_name
       B.total_books number_of_books_written
   from authors A
   inner join (
    select top 1 
        author_id
        , count(*) [total_books] 
    from books 
    group by author_id 
    order by count(*) desc
    ) B on A.author_id = B.author_id

抱歉格式化,我认为这是我第一次提交的回复。