【问题标题】:Need help with a conditional SELECT statement在条件 SELECT 语句方面需要帮助
【发布时间】:2011-02-13 22:16:24
【问题描述】:

我有一个带有 select 语句的存储过程,如下所示:

SELECT author_ID,
       author_name,
       author_bio
FROM   Authors
WHERE author_ID in (SELECT author_ID from Books)

这会将结果限制为拥有书籍记录的作者。这是 Books 表:

Books:
book_ID INT,
author_ID INT,
book_title NVARCHAR,
featured_book BIT

我想要做的是有条件地选择每个作者的特色书的 ID 作为上面 select 语句的一部分,如果给定作者的书都没有特色,则选择第一本书的 ID(前 1 ) 由书表中的作者撰写的书。我该如何处理?

【问题讨论】:

  • 你所说的“第一本”书是什么意思?由什么命令的?你的意思是获取任何本书吗?
  • 正确 - 实际上,该作者的任何书籍。我的意思是按 book_ID 排序的前 1 个。

标签: sql sql-server select conditional


【解决方案1】:

您可以使用对数据进行排序并使用顶部语句的子查询...

类似的东西,

SELECT  author_ID, 
         author_name, 
         author_bio
         , (Select top 1 Book_ID from Books where Authors.Author_ID=Books.Author_ID order by Featured_book desc)
 FROM    Authors 
 WHERE author_ID in (SELECT author_ID from Books) 

【讨论】:

    【解决方案2】:

    试试这个:

    DECLARE @Authors table (author_ID INT NOT NULL, author_name VARCHAR(100) NOT NULL, author_bio VARCHAR(100) NOT NULL)
    INSERT INTO @Authors VALUES (1, 'Author1', 'Bio1')
    INSERT INTO @Authors VALUES (2, 'Author2', 'Bio2')
    INSERT INTO @Authors VALUES (3, 'Author3', 'Bio3')
    
    DECLARE @Books table (book_ID INT NOT NULL, author_ID INT NOT NULL, book_title VARCHAR(100) NOT NULL, featured_book INT NOT NULL)
    INSERT INTO @Books VALUES (1, 2, 'Book1', 0)
    INSERT INTO @Books VALUES (2, 2, 'Book2', 1)
    INSERT INTO @Books VALUES (3, 3, 'Book3', 0)
    INSERT INTO @Books VALUES (4, 3, 'Book4', 0)
    
    SELECT
        dt.author_ID, dt.author_name,dt.author_bio,dt.book_title
        FROM (
              SELECT
                  a.*,b.book_title,ROW_NUMBER() OVER (PARTITION by a.author_ID ORDER BY b.featured_book DESC,b.book_title) RowNumber
    
              FROM Authors              a
                  LEFT OUTER JOIN Books b ON a.author_id = b.author_id
              ) dt
    WHERE dt.RowNumber= 1
    

    输出:

    author_ID   author_name  author_bio  book_title
    ----------- ------------ ----------- ----------
    1           Author1      Bio1        NULL      
    2           Author2      Bio2        Book2     
    3           Author3      Bio3        Book3      
    
    (3 row(s) affected)
    

    【讨论】:

    • 据我所知,没有书的作者是不受欢迎的。 This limits results to authors who have book records.
    • @Mark Byers,我没有那样读这个问题。您的相当不是问题的“我想要什么......”部分的一部分。 OP只提到他们有一个存储过程......这限制......等等。
    【解决方案3】:
    Select X.*, BB.book_title from 
    (
        SELECT A.author_ID, A.author_name, A.author_bio,
          (Select top 1 book_ID from Books B WHERE B.author_ID = A.author_ID
            order by B.featured_book, book_Title) as book_ID, BB.book_Title
        FROM   Authors A 
    )X right join Books BB on X.book_id = BB.book_ID
    

    【讨论】:

      【解决方案4】:

      理论上,关系没有顺序。因此,理想情况下,“第一本书”应该由一些聚合规则指定。如果您对“min(bookID)”感到满意,那么类似:

      SELECT Authors.author_ID, author_name, author_bio,
        CASE max(featured_book)
        WHEN 0 THEN min(book_ID)
        END
      FROM Authors INNER JOIN
           Books ON Authors.author_ID = Books.author_ID
      GROUP BY Authors.author_ID, author_name, author_bio
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多