Mysql获得独特的计数行

时间:2015-05-15 15:00:20

标签: php mysql sql database

我有下表:

| id   | userid | level | amount | workid | time |
| 1    |   1    |   0   |  209   |   1    | 283  |
| 2    |   1    |   1   |  423   |   2    | 283  |
| 3    |   1    |   0   |  4209  |   1    | 283  |

如何根据级别和工作ID获得任何组合?

我想回复以下内容:

level | amount | workid |
   0  |  4418  |  1     |
   1  |  423   |  2     |

这意味着它将计算特定级别和工作ID的金额,并根据工作ID和级别继续提供其他组合。

Userid始终包含变量$x,在这种情况下为1

3 个答案:

答案 0 :(得分:4)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="comp_class">
  <label class='label_1 '>Competition</label>
  <div class="control_1">
    <div class="checkbox">
      <label>
        <input type='checkbox' name='field_1' value='Yes' required />Do you agree?</label>
      <label for="field_1" class="error"></label>
    </div>
  </div>
  <button id='submit_button'>Submit</button>
</div>

您可以根据您使用的代码将SELECT level,SUM(amount),workid FROM yourTable WHERE userid=1 GROUP BY level,workid 变为变量

<强> Fiddle

答案 1 :(得分:0)

您可以通过一些简单的聚合来完成此操作。您需要添加的金额是SUM()功能,您可以按级别和工作组进行分组。然后,使用WHERE子句过滤掉用户1:

SELECT level, SUM(amount) AS totalAmount, workid
FROM myTable
WHERE userid = 1
GROUP BY level, workid;

以下是SQL Fiddle示例。

答案 2 :(得分:-1)

简单,

SELECT level,SUM(amount) AS amount,workid FROM table WHERE userId=1
GROUP BY level,workid;