› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
troyl
V2EX  ›  MySQL

一个 MySql 的问题

  •  
  •   troyl · Apr 26, 2014 · 4117 views
    This topic created in 4536 days ago, the information mentioned may be changed or developed.
    我现在有一个表如下图所示:



    documentID 是文章号码
    term 是词语
    score 是词语在文章中出现的次数

    当我执行
    select documentID, score from myIndex where term='children' or term='infant';
    结果如下:

    可以看见有很多 documentID 的重复项,比如第二行和第三行的 documentID 就是一样的

    123 5
    123 3

    有没有办法让 documentID 变成一个,而 score 的值是重复项之和?
    拿第二行和第三行来说,就是:
    123 8

    先行谢过~
    11 replies  •  1970-01-01 08:00:00 +08:00
    patosky
        1
    patosky  
       Apr 26, 2014
    count(score) group by documentID
    patosky
        2
    patosky  
       Apr 26, 2014
    @patosky sum()吧。。。写错了
    troyl
        3
    troyl  
    OP
       Apr 26, 2014
    @patosky select documentID, sum(score) as orScore from myIndex where term='children' or term='infant' group by documentID;
    troyl
        4
    troyl  
    OP
       Apr 26, 2014
    @patosky 谢谢!
    troyl
        5
    troyl  
    OP
       Apr 26, 2014
    @patosky 这次学到了 group by,以前都没接触过==
    Mac
        6
    Mac  
       Apr 26, 2014
    @troyl 那可以再学学 group_concat,这货也很有用
    coolicer
        7
    coolicer  
       Apr 26, 2014
    你用concat_group和group by将它分组,重复的用distinct去掉。结果出来了再去自己计算。

    大概是这样的结果:
    123 5,3
    xxx 1,2,3,4
    ...

    只能帮到这里了
    coolicer
        8
    coolicer  
       Apr 26, 2014
    打完发现已经有几个回答了 = =
    troyl
        9
    troyl  
    OP
       Apr 26, 2014
    @patosky 顺便再问一个问题,多个数的和可以用 sum() 函数
    那么如果想求多个数的积呢?
    patosky
        10
    patosky  
       Apr 26, 2014   ❤️ 1
    @troyl select documentID, cast(exp(sum(log(score))) as SIGNED) from myIndex group by documentID
    troyl
        11
    troyl  
    OP
       Apr 26, 2014
    @patosky 谢谢啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2754 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:22 · PVG 10:22 · LAX 19:22 · JFK 22:22
    ♥ Do have faith in what you're doing.