MySQL 一对多,仅从多个表中提取选定的记录

发布于 2024-11-24 00:41:40 字数 764 浏览 0 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(4

仙气飘飘 2024-12-01 00:41:40
select DISTINCT m.* from music m INNER JOIN reviews r on m.uid = r.uid where r.thumbsup > 0 
select DISTINCT m.* from music m INNER JOIN reviews r on m.uid = r.uid where r.thumbsup > 0 
爱已欠费 2024-12-01 00:41:40

像这样的事情怎么样:

SELECT m.*, IFNULL((SELECT 1 FROM reviews r WHERE r.thumbsup > 0 AND m.uid = r.uid LIMIT 1),0) has_review 
FROM music m

How about something like this :

SELECT m.*, IFNULL((SELECT 1 FROM reviews r WHERE r.thumbsup > 0 AND m.uid = r.uid LIMIT 1),0) has_review 
FROM music m
2024-12-01 00:41:40
SELECT m.* 
FROM music m
INNER JOIN 
  (SELECT DISTINCT reviews.uid WHERE review.thumbsup > 0) r ON (r.uid = m.uid)
SELECT m.* 
FROM music m
INNER JOIN 
  (SELECT DISTINCT reviews.uid WHERE review.thumbsup > 0) r ON (r.uid = m.uid)
無心 2024-12-01 00:41:40
SELECT DISTINCT m.*, r.thumbsup
FROM music m LEFT JOIN reviews r 
ON (m.uid = r.uid AND r.thumbsup=1);

将返回没有评论、没有正面评论、混合评论、仅正面评论的音乐记录(注明任何正面评论)

SELECT DISTINCT m.*, r.thumbsup
FROM music m LEFT JOIN reviews r 
ON (m.uid = r.uid AND r.thumbsup=1);

Will return music records with no reviews, no positive reviews, mixed reviews, postivie only reviews (with any positive reviews indicated)

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