如何在SQL查詢中搜索多個條目?

[英]How to search for multiple entries in SQL Query?


When trying to search for one book title's number of copies in every branch in my Library Database this works great:

當試圖在我的圖書館數據庫的每個分支中搜索一個圖書標題的數量時,這很有用:

select BranchName, Title, sum(No_Of_Copies) as NumberOfCopies
    from Library_Branch lb
    join Book_Copies bc
    on lb.BranchId = bc.BranchId
    join BOOK b
    on bc.BookId = b.BookId WHERE Title like '%The Great Gatsby%'
    group by lb.BranchName, b.Title

But when I try to search for two books I get an error:

但是當我試圖尋找兩本書時,我發現了一個錯誤:

select BranchName, Title, sum(No_Of_Copies) as NumberOfCopies
    from Library_Branch lb
    join Book_Copies bc
    on lb.BranchId = bc.BranchId
    join BOOK b
    on bc.BookId = b.BookId WHERE Title like '%The Great Gatsby%' , '%The Chronicles of Narnia'
    group by lb.BranchName, b.Title

I have also tried:

我也試過:

select BranchName, Title, sum(No_Of_Copies) as NumberOfCopies
    from Library_Branch lb
    join Book_Copies bc
    on lb.BranchId = bc.BranchId
    join BOOK b
    on bc.BookId = b.BookId WHERE Title like '%The Great Gatsby%' or '%The Chronicles of Narnia'
    group by lb.BranchName, b.Title

I know this is a very beginner question but I am quite new to SQL. Any help would be greatly appreciated, thank you.

我知道這是一個非常初級的問題,但我對SQL很陌生。非常感謝您的幫助,謝謝。

2 个解决方案

#1


1  

You have to totally rewrite the where condition after the OR:

必須重寫OR后的where條件:

select BranchName, Title, sum(No_Of_Copies) as NumberOfCopies
    from Library_Branch lb
    join Book_Copies bc
    on lb.BranchId = bc.BranchId
    join BOOK b
    on bc.BookId = b.BookId WHERE Title like '%The Great Gatsby%' or Title like '%The Chronicles of Narnia'
    group by lb.BranchName, b.Title

#2


1  

You were close, try this:

你很接近,試試這個:

SELECT BranchName, Title, sum(No_Of_Copies) as NumberOfCopies
FROM Library_Branch lb
JOIN Book_Copies bc on lb.BranchId = bc.BranchId
JOIN BOOK b on bc.BookId = b.BookId 
WHERE Title LIKE '%The Great Gatsby%' or Title LIKE '%The Chronicles of Narnia'
GROUP BY lb.BranchName, b.Title

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/01/10/729fc734fad29df1c76e0e1f54fc448a.html