0

我有桌子:

==========================================================
|id | before | after | freq | id_sentence | document_id  |
==========================================================
| 1 |    a   |   b   | 1    |     0       |      1       |
| 2 |    c   |   d   | 1    |     1       |      1       |
| 3 |    e   |   f   | 1    |     1       |      1       |
| 4 |    g   |   h   | 2    |     0       |      2       |
==========================================================

我想获得数据的数量取决于id_sentencefreq所以结果必须是 1 2 1 这是代码:

$query = mysql_query("SELECT freq FROM tb where document_id='$doc_id' ");
    while ($row = mysql_fetch_array($query)) {
        $stem_freq = $row['freq'];
        $total = $total+$stem_freq;

但结果仍然是错误的。请帮帮我..谢谢:)

4

2 回答 2

2

如果我理解您的问题,您正在尝试计算freq每个不同id_sentence的特定的总和document_id

尝试以下 SQL:

SELECT id_sentence, SUM(freq) as freq FROM tb WHERE document_id = 1 GROUP BY(id_sentence)

结果将是具有id_sentence相应总和的数据行freq。事后无需手动总结。

请参阅此 SQL 小提琴:http ://sqlfiddle.com/#!2/691ed/8

于 2012-09-06T03:33:58.117 回答
1

我认为你可以做类似的事情

SELECT count(*) AS count FROM tb GROUP BY(id_sentence,freq)

做你想要的计数。你甚至可以做类似的事情

SELECT count(*) AS count, id_sentence, freq FROM tb GROUP BY(id_sentence,freq)

知道计数是针对哪个 id_sentence,freq 组合。

于 2012-09-06T03:16:22.180 回答