![](/img/trans.png)
[英]How to: Find and update all the entries where the value in one column shows up more than once
[英]Get ALL rows where more than X entries for column value
我在MySQL數據庫中有一個帶有標題和作者列的書表。 我需要選擇表中具有X個以上作者的Author的所有書籍。
這是所需輸出的示例:
| Author | Title |
|---------------|----------------------|
| Dan Brown | Angels and Demons |
| Dan Brown | The Da Vinci Code |
| Robert Ludlum | The Bourne Identity |
| Robert Ludlum | The Bourne Supremacy |
我可以通過一個簡單的小組並有條件地為每個作者獲得一本書:
SELECT * FROM books
GROUP BY author
HAVING count(author) > 1
ORDER BY author;
給出以下輸出:
| Author | Title |
|---------------|----------------------|
| Dan Brown | Angels and Demons |
| Robert Ludlum | The Bourne Identity |
如何獲得作者擁有超過X本書的書籍清單?
您可以將查詢用作子選擇 :
SELECT * FROM books WHERE Author IN (
SELECT Author
FROM books
GROUP BY Author
HAVING COUNT(Author) > 1
) ORDER BY Author, Title;
如果您想獲得每個作者的簡單(逗號分隔)書籍清單,您還可以使用GROUP_CONCAT
使用以下解決方案:
SELECT Author, GROUP_CONCAT(Title) AS title_list
FROM books
GROUP BY Author
HAVING COUNT(Author) > 1
ORDER BY Author;
您應該可以使用子查詢 :
SELECT * FROM books
WHERE author IN (
SELECT author
FROM books
GROUP BY author
HAVING COUNT(author) > 1
)
也許嘗試使用...
SELECT [Author] , [Title]
FROM [books] b
where [Author] in
(select Author from [books] group by [Author]
having count(Author) >1)
一個非常簡單的查詢將如下所示
SELECT books.Title
FROM (SELECT Count(books.Author) AS CountOfAuthor, books.Author AS A
FROM books
GROUP BY books.Author ) AS bookByAuthor INNER JOIN books ON bookByAuthor.A =
books.Author
WHERE (((books.Author)=[bookByAuthor].[A]));
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.