SQL Server 2008 分组依据

发布于 2024-11-24 04:47:13 字数 1047 浏览 1 评论 0原文

我被困住了,我正在寻找解决方案,所以请帮忙!

我有 3 个表:

Books (id, title, ...)

Authors (id, name, surname, ...)

book_authors (bookID, authorID, whatisdoing)

现在我想根据标题(用户搜索)和所有其他信息(作者姓名、姓氏)检索所有书籍。但是,我希望书名是唯一的,只显示第一次出现的 book_authors.whatisdoing

在 MS Access 中,我使用 first 函数实现了这一点,但现在 first 不起作用,并且使用 min 我没有得到我想要的结果。

任何帮助将不胜感激。

Access 中的查询是:

SELECT 
    First(book_authos.whatisdoing) AS FirstOfidiothta_ID, 
    First(authors.name) AS onoma, 
    First(authors.surname) AS eponymo, 
    books.ID, books.title, books.photoLink
FROM (books 
INNER JOIN book_authors ON books.ID = book_authors.book_ID) 
INNER JOIN authors ON book_authors.author_ID = authors.ID 
GROUP BY 
    books.ID, books.titlos, books.photoLink, books.active 
HAVING 
    (((books.titlos) Like '%" & textString & "%') AND 
    ((books.active)=True) AND ((First(authors.active))=True)) 
ORDER BY 
    First(book_authos.whatisdoing), books.title

I am stuck and I am trying to find a solution, so please help!

I have 3 tables:

Books (id, title, ...)

Authors (id, name, surname, ...)

book_authors (bookID, authorID, whatisdoing)

Now I want to retrieve all books depending on the title (user search) and all the other info (author name, surname). But, I want the book titles to be unique with only the first occurrence of the book_authors.whatisdoing to be shown.

In MS Access I achieved that with first function, but now first does not work and with min I didn't get the results I want.

Any help would be appreciate.

The query in Access was:

SELECT 
    First(book_authos.whatisdoing) AS FirstOfidiothta_ID, 
    First(authors.name) AS onoma, 
    First(authors.surname) AS eponymo, 
    books.ID, books.title, books.photoLink
FROM (books 
INNER JOIN book_authors ON books.ID = book_authors.book_ID) 
INNER JOIN authors ON book_authors.author_ID = authors.ID 
GROUP BY 
    books.ID, books.titlos, books.photoLink, books.active 
HAVING 
    (((books.titlos) Like '%" & textString & "%') AND 
    ((books.active)=True) AND ((First(authors.active))=True)) 
ORDER BY 
    First(book_authos.whatisdoing), books.title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-12-01 04:47:13

编辑:根据OP评论进行更改。
编辑2:修改以纠正缺陷。

你可以尝试一下。

SELECT aba.name, aba.surname, aba.whatisdoing,
    b.ID, b.title, b.photoLink
FROM Books b
JOIN
    (
    SELECT ba.bookID, ba.whatisdoing, a.name, a.surname,
      ROW_NUMBER() OVER (PARTITION BY ba.bookID ORDER BY ba.whatisdoing) AS sequence
    FROM book_authors ba
    JOIN Authors a ON ba.authorID = a.id
    WHERE a.active = 1
    ) AS aba ON b.id = aba.bookID
WHERE b.title LIKE '%' + @textString + '%'
    AND b.active = 1
    AND aba.sequence = 1

ROW_NUMBER 函数根据 PARTITION BY 子句中定义的组和 ORDER BY 子句定义的顺序为行分配行号。

@textString 是一个 SQL Server 变量。我不确定在你的情况下你会如何分配这个。

在 SQL Server 中,布尔数据类型为 BIT,值为 0 表示 false,1 表示 true。

EDIT: Changed based on OP comments.

EDIT 2: Revised to correct flaws.

You might give this a try.

SELECT aba.name, aba.surname, aba.whatisdoing,
    b.ID, b.title, b.photoLink
FROM Books b
JOIN
    (
    SELECT ba.bookID, ba.whatisdoing, a.name, a.surname,
      ROW_NUMBER() OVER (PARTITION BY ba.bookID ORDER BY ba.whatisdoing) AS sequence
    FROM book_authors ba
    JOIN Authors a ON ba.authorID = a.id
    WHERE a.active = 1
    ) AS aba ON b.id = aba.bookID
WHERE b.title LIKE '%' + @textString + '%'
    AND b.active = 1
    AND aba.sequence = 1

The ROW_NUMBER function assigns a row number to the row based on the groups defined in the PARTITION BY clause and the order defined by the ORDER BY clause.

@textString is a SQL Server variable. I'm not sure how you would assign this in your situation.

The boolean data type is BIT in SQL Server and the values are 0 for false and 1 from true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文