带有主查询数据变量的 MySQL 子查询
好的,这里需要一位 MySQL 专家。我正在尝试编写一个查询,当有人对您之前评论过的项目发表评论时,该查询将用作通知系统。 “drinkComment”表非常简单:
commentID, userID, drinkID, datetime, comment
我编写了一个查询,它将获取我之前评论过的饮料的所有评论(不是我的),但它仍然会显示在我发表评论之前发生的评论。这与我认为可行的最接近,但事实并非如此。请帮忙!
select @drinkID:=drinkComments.drinkID, commentID, drinkID, userID, comment, datetime
FROM drinkComments
WHERE `drinkID` IN
( select distinct drinkID from drinkComments where drinkComments.userID = 1)
AND drinkComments.dateTime > (
/*This gets the last date user commented on the main query's drinkID*/
select datetime FROM drinkComments WHERE drinkComments.userID = 1 AND drinkComments.drinkID = @drinkID ORDER BY datetime DESC LIMIT 1
)
ORDER BY datetime DESC
Ok, need a MySQL guru here. I am trying to write a query that will serve as a notification system for when someone leaves a comment on an item that you have previously commented on. The 'drinkComment' table is very simple:
commentID, userID, drinkID, datetime, comment
I've written a query that will get all of the comments on drinks that I have previously commented on (that are not mine), but it will still show comments that occurred BEFORE my comment. This is as close to what I would think would work, but it does not. Please help!
select @drinkID:=drinkComments.drinkID, commentID, drinkID, userID, comment, datetime
FROM drinkComments
WHERE `drinkID` IN
( select distinct drinkID from drinkComments where drinkComments.userID = 1)
AND drinkComments.dateTime > (
/*This gets the last date user commented on the main query's drinkID*/
select datetime FROM drinkComments WHERE drinkComments.userID = 1 AND drinkComments.drinkID = @drinkID ORDER BY datetime DESC LIMIT 1
)
ORDER BY datetime DESC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不从用户的预查询以及他们提供评论的所有饮料以及截止时间开始(不知道您是否对任何给定的饮料每人有多个评论)。然后,在您的日期/时间评论之后查找所有其他人的评论...
此查询实际上应该更快,因为它仅以一个用户的饮料评论作为基础开始,然后返回评论表以查找与饮料匹配的评论ID和截止时间。
Why not start with a prequery of the user and all the drinks they've offered comments and as of what time (don't know if you have multiple comments per person for any given drink or not). Then, find comments from all others AFTER such of your date/time comment...
This query should actually be faster as it is STARTING with only ONE USER's drink comments as a basis, THEN goes back to the comments table for those matching the drink ID and cutoff time.
我认为您需要通过 DrinkID 将最里面的查询与中间的查询相关联。
I think you need to relate your innermost query to the middle query by drinkID.