MySQL 一对多,仅从多个表中提取选定的记录
我对 SQL 语句很陌生,而且我从来都不擅长连接等。我在解决如何执行以下操作时遇到问题:
我有 2 个表,一个称为 MUSIC,一个称为 REVIEWS。两个表链接在一个名为 uid 的字段上 - MUSIC 中的记录是唯一的,但每个 MUSIC 记录可能有许多评论。
REVIEWS 有一个名为thumbsup 的字段,如果用户对音乐竖起大拇指,则该字段设置为值1。否则,该字段为零。 REVIEWS中可能有很多点赞,也可能只有一个点赞,或者没有点赞。
我正在尝试显示 MUSIC 中的记录列表,如果有相应的 REVIEW 记录包含拇指值 1 - 只需 REVIEWS 中的一条记录即可 - 然后在列表页面上显示一个图标。
我仔细研究了各种类似的问题,但没有出现类似的问题。我尝试过对类似类型的连接进行修改,但坦率地说,它是如何工作的超出了我的范围。抱歉我是个白痴。
作为我至少尝试过的证据,到目前为止,我已经根据另一篇文章提出了这个:
SELECT m.*, r.thumbsup
FROM (SELECT MAX(thumbsup) uid FROM reviews GROUP By uid) maxThumbs
INNER JOIN reviews r ON maxThumbs.uid = reviews.uid
INNER JOIN music m ON music.uid = reviews.uid
不用说它不起作用:(
如果有人可以帮助我在这里使用正确的查询结构,我会非常感
谢特德
。
I am quite new at SQL statements and I have never been very good at joins etc. I am having a problem working out how to do the following:
I have 2 tables, one called MUSIC, one called REVIEWS. The two tables link on a field called uid - records in MUSIC are unique, but there may be many reviews for each MUSIC record.
REVIEWS has a field called thumbsup, which is set to value 1 if a user gives a MUSIC the thumbs up. Otherwise, the field is zero. There may be many thumbsup in REVIEWS, one thumbsup, or no thumbsup.
I am trying to display a list of records in MUSIC, and if there is a corresponding REVIEW record that contains a thumbsup value of 1 - just one record in REVIEWS will do it - then to display an icon on the list page.
I have had a good look around various similar questions but nothing quite like this comes up. I have tried doctoring around similar kinds of joins, but frankly how it all works is beyond me. Sorry for being an idiot.
By way of evidence that I have at least had a go, so far I have come up with this based on another post:
SELECT m.*, r.thumbsup
FROM (SELECT MAX(thumbsup) uid FROM reviews GROUP By uid) maxThumbs
INNER JOIN reviews r ON maxThumbs.uid = reviews.uid
INNER JOIN music m ON music.uid = reviews.uid
Needless to say it doesn't work :(
If anyone could help me out here with the right query structure I would be extremely grateful.
Many thanks
Ted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的事情怎么样:
How about something like this :
将返回没有评论、没有正面评论、混合评论、仅正面评论的音乐记录(注明任何正面评论)
Will return music records with no reviews, no positive reviews, mixed reviews, postivie only reviews (with any positive reviews indicated)