MYSQL 查询产生重复行之一的 JOIN

发布于 2024-12-29 08:59:24 字数 1526 浏览 2 评论 0原文

编辑:非常感谢谢尔盖为我解决了这个问题。一旦他得到答案,我就会标记为并回答。

我的查询如下所示:

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

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

发布评论

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

评论(1

最丧也最甜 2025-01-05 08:59:24

LEFT JOINed 表中有重复的 Song_id。我使用类似以下内容的方法将 JOIN 限制为只有一个匹配记录:

已编辑:

LEFT JOIN $sTable6 gs ON gs.song_id = songsID AND gs.MYUNIQUECOLUMN IN (SELECT MYUNIQUECOLUMN FROM $sTable6 WHERE song_id = songsID ORDER BY MYUNIQUECOLUMN LIMIT 1)

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:

LEFT JOIN $sTable6 gs ON gs.song_id = songsID AND gs.MYUNIQUECOLUMN IN (SELECT MYUNIQUECOLUMN FROM $sTable6 WHERE song_id = songsID ORDER BY MYUNIQUECOLUMN LIMIT 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文