MYSQL 查询产生重复行之一的 JOIN
编辑:非常感谢谢尔盖为我解决了这个问题。一旦他得到答案,我就会标记为并回答。
我的查询如下所示:
SELECT SQL_CALC_FOUND_ROWS
songsID, song_name, artist_band_name, author, song_artwork, song_file,
genre, song_description, uploaded_time, emotion, tempo,
user, happiness, instruments, similar_artists, play_count,
projects_count,
rating, ratings_count, waveform, datasize, display_name, user_url, genre_id,
IF(user_ratings_count, 'User Voted', 'Not Voted') as voted
FROM (
SELECT
sp.songsID, projects_count,
AVG(rating) as rating,
COUNT(rating) AS ratings_count,
COUNT(IF(userid=$userid, 1, NULL)) as user_ratings_count
FROM (
SELECT songsID, COUNT(*) as projects_count
FROM $sTable s
LEFT JOIN $sTable2 p ON s.songsID = p.songs_id
GROUP BY songsID) as sp
LEFT JOIN $sTable3 r ON sp.songsID = r.songid
GROUP BY sp.songsID) as spr
LEFT JOIN $sTable6 gs ON gs.song_id = songsID
JOIN $sTable s USING (songsID)
LEFT JOIN $sTable5 q ON s.user = q.ID
虽然此查询返回各个表中的所有信息(全部指定为 $sTable、$sTable2、$sTable3 等),但它给出了基于 $sTable6 的重复行。
仅当 $sTable6 JOIN 就位时才会发生这种情况,即倒数第三行:
LEFT JOIN $sTable6 gs ON gs.song_id = songsID
删除此行后一切正常。但是,我需要检索此信息,以便通过“genre_id”提供动态过滤。目前,它检索 $sTable 中的所有行以及其他各个表中的所有相应信息,但还会重新打印 $sTable 中与 $sTable6 共享 Song_id/songsID 的任何行。
我怎样才能防止这种情况发生?
EDIT: Huge thanks to Sergey for solving this problem for me. As soon as he gets it put up as an answer I will mark is and answered.
My query is as shown below:
SELECT SQL_CALC_FOUND_ROWS
songsID, song_name, artist_band_name, author, song_artwork, song_file,
genre, song_description, uploaded_time, emotion, tempo,
user, happiness, instruments, similar_artists, play_count,
projects_count,
rating, ratings_count, waveform, datasize, display_name, user_url, genre_id,
IF(user_ratings_count, 'User Voted', 'Not Voted') as voted
FROM (
SELECT
sp.songsID, projects_count,
AVG(rating) as rating,
COUNT(rating) AS ratings_count,
COUNT(IF(userid=$userid, 1, NULL)) as user_ratings_count
FROM (
SELECT songsID, COUNT(*) as projects_count
FROM $sTable s
LEFT JOIN $sTable2 p ON s.songsID = p.songs_id
GROUP BY songsID) as sp
LEFT JOIN $sTable3 r ON sp.songsID = r.songid
GROUP BY sp.songsID) as spr
LEFT JOIN $sTable6 gs ON gs.song_id = songsID
JOIN $sTable s USING (songsID)
LEFT JOIN $sTable5 q ON s.user = q.ID
While this query is returning all the information from the various tables (all designates as $sTable, $sTable2, $sTable3 etc.) it is giving me duplicate rows based on $sTable6.
This only occurs with the $sTable6 JOIN in place, the line third from the bottom:
LEFT JOIN $sTable6 gs ON gs.song_id = songsID
With this line removed all is okay. However I need to retrieve this information in order to provide dynamic filtering by the 'genre_id'. Currently it retrieves all the rows from $sTable along with all the corresponding information from the other various tables, but also reprints any rows in $sTable that share the song_id/songsID with $sTable6.
How can I prevent this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LEFT JOINed 表中有重复的 Song_id。我使用类似以下内容的方法将 JOIN 限制为只有一个匹配记录:
已编辑:
You have duplicate song_id's in the LEFT JOINed table. I use something like the following to limit that JOIN to only one matching record:
EDITED: