dtkyayvldeaqhl7151 2012-04-25 07:03
浏览 90
已采纳

如何在MySQL数据库中查找最常用的单词并平均第二列

So I have two columns of text in a MySQL database, an example would be as follows:

Name             Score
Henry Hodgens    4
Mary Hodgens     8
Jim Servan       2
Jane Servan      4
Hank Servan      6
Sarah Smith      10
Mary Smith       12
Henry Dobbins    2
Henry Jenkins    4

I need to run a query with PHP that can show the average of "Score", based on the most common occurrences of a single word in "Name". So, it would show that "Servan" averages 4, "Henry" averages 3.3, "Hodgens" averages 6, "Mary" averages 10, in the order of most occurrences of the word in "Name".

I hope this makes sense.

  • 写回答

4条回答 默认 最新

  • dongqufi82315 2012-04-25 07:30
    关注

    You could do it like this:

    SELECT
        AVG(t.Score) AS ScorceAvg,
        t.name
    FROM
        (
            SELECT 
                SUBSTRING(Table1.Name,1,INSTR(Table1.Name, ' ')) AS name,
                Table1.Score
            FROM 
                Table1
            UNION ALL
            SELECT 
                SUBSTRING(Table1.Name,INSTR(Table1.Name, ' ')) AS name,
                Score
            FROM 
                Table1
        ) AS t
    GROUP BY
        t.name
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug