SQL Server 2008 分组依据
我被困住了,我正在寻找解决方案,所以请帮忙!
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:根据OP评论进行更改。
编辑2:修改以纠正缺陷。
你可以尝试一下。
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.
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.