douyong4842 2013-10-14 17:48
浏览 33
已采纳

MySQL多个重复行 - 只返回一个

I imported my iTunes Library into a table via a script. The script doesn't catch any duplicate song title and artists. Therefore, I have many duplicate songs.

trackid    song    artist   tracknum
4628       Title1  StackO      2
6846       YMCA    (blank)     11
9043       YMCA    (blank)     11
9381       YMCA    (blank)     11
9382       Title2  StackO      3

How would I write my SQL statement to return only the FIRST "trackid" of YMCA (4628) row when the song, artist and tracknum are all the same? It should look like this....

trackid    song    artist   tracknum
4628       Title1  StackO      2
6846       YMCA    (blank)     11
9382       Title2  StackO      3

Here is my SQL as it stands right now...

"SELECT DISTINCT * FROM wp_tracks WHERE title LIKE '%" . $song . "%' AND artist LIKE '%" . $artist . "%'

This has been baffling me for awhile now.

Thank you.

  • 写回答

2条回答 默认 最新

  • duanmao9918 2013-10-14 17:52
    关注
    SELECT song, artist, tracknum, MIN(trackid) AS first_track_id
    FROM wp_tracks
    GROUP BY song, artist, tracknum
    

    Add a WHERE clause to choose which tracks you want listed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起