如何首先返回具有特定值的行,并按字母顺序依赖表的所有值

时间:2018-02-17 23:38:39

标签: java sql database oracle11g oracle-sqldeveloper

我需要按字母顺序获取book_namebook_code订单by book_name

我有三张桌子:

表1

Books (Independent Table which will have all the books)

表2

Authors (Independent Table which will have all the authors)

表3

Author_Books(This table will provide the information about which book is which author's primary book.) 

注意:一位作者可以拥有多本书。

我的查询输入为 Author_code

  1. 如果我传递Author_code,我需要获得该主要书籍      作为第一行的作者并且休息表中的所有书籍应按book_name以字母顺序排列

  2. 如果我没有将任何输入传递给我的查询,我需要所有的书籍     字母顺序。

  3. 我的查询返回作者的默认图书:

    SELECT book_CD, book_NAM 
    from books 
    where Alpha_NUM_book_CD 
               = (SELECT Alpha_NUM_book_CD 
                  from Author_Book
                  JOIN Author 
                  on Author.NUM_Author_CD = Author_Book.NUM_Author_CD 
                   where Author.NUM_Author_CD
                          = (SELECT NUM_Author_CD 
                             from Author 
                             where ALPHA_3_Author_CD = '') 
                  AND Author_Book.PRIM_book_SW = 'Y'
       );
    

    如何编写满足我的案例的查询?

    我们正在使用Oracle 11g。

1 个答案:

答案 0 :(得分:0)

order by可以使用多个键。所以,像这样:

order by (case when author.NUM_Author_CD = ? then 1 else 2 end),
         book.title
相关问题