2

我有桌子:

=========================================================================
| id | stem_before | stem_after | stem_freq | sentence_id | document_id | 
=========================================================================
|  1 |     a       |     b      |    1      |   0         |       1     |    
|  2 |     c       |     d      |    1      |   0         |       1     |        
|  3 |     e       |     f      |    1      |   1         |       1     |
|  4 |     g       |     h      |    1      |   2         |       1     |
|  5 |     i       |     j      |    2      |   0         |       2     |
|  6 |     k       |     l      |    1      |   0         |       2     |
=========================================================================

我想分两步计算:第一步是将 1 除以stem_freq每个sentence_idand中的值的总和document_id。第二步是将第一步的结果与值相乘stem_freq

例如 :

对于document_id= 1 和sentence_id= 0 的数据,第一步:1/(1+1)= 0.5,第二步id= 1 为1*0.5= 0.5。对于id= 2 是1*0.5= 0.5。

对于document_id= 2 和sentence_id= 0 的数据,第一步:1/(2+1)= 0.3333,第二步id= 5 是2*0.3333= 0.6666,对于id= 6 是1*0.3333= 0.3333。

这是我的代码:

$query = mysql_query ("SELECT sentence_id, document_id, stem_after, 
stem_freq,SUM(stem_freq) as freq 
FROM tb_stemming 
WHERE document_id ='$doc_id' 
GROUP BY(sentence_id)");

while ($row = mysql_fetch_array($query)) {
   $a    = $row['freq'];
   $freq = $row['stem_freq'];
   $tf   = $freq/$a;
}

但它只给了我每个不同句子中第一个数据的结果:你能帮帮我吗?谢谢你 :)

4

1 回答 1

1

试试这个:

SELECT
    a.*, 
    a.stem_freq * b.value
FROM
    tb_stemming as a
    JOIN 
    (
        SELECT
            document_id,
            sentence_id,
            1 / sum(stem_freq) 'value'
        FROM
            tb_stemming
        GROUP BY document_id, sentence_id
    ) as b
    ON a.document_id = b.document_id AND a.sentence_id = b.sentence_id
于 2012-09-10T03:41:10.823 回答