drt96856 2012-07-01 09:37
浏览 77
已采纳

MySQL - 获取列中的平均值

I'm making a list with certain songs. Each song has its own unique ID. OK let's say I have this table called list. A new:

enter image description here

The ID is self-explanatory. It's used to ID rows. song_one through song_ten is filled with a song's unique ID.

Every time a user makes a new list, they add songs and each row gets filled. Now, how would I get the average rank of the songs in the tables list using the song's ID that is filled between song_one through song_ten?

  • 写回答

3条回答 默认 最新

  • dpt62283 2012-07-01 09:42
    关注

    Redesign your database. Make a new table with songid, listid and rank. This will make your job easy.

    listsongs
    -----------------
    songid [PK] -- Foreign key referencing the songs table
    listid [PK] -- Foreign key referencing the lists table
    rank
    

    Selecting average song ranks:

    SELECT
        a.song_id
        AVG(b.rank) AS avgrank
    FROM
        songs a
    LEFT JOIN
        listsongs b PM a.song_id = b.song_id
    GROUP BY
        a.song_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)