MySQL Complex Query(分组依据)

时间:2016-12-09 13:15:44

标签: mysql

以下是我的询问:

SELECT date, eventype, count(*) AS count 
FROM vpnhistory WHERE partnername   REGEXP 'CESAR'
GROUP BY (UNIX_TIMESTAMP(date)) DIV 600;

我得到了什么:

+---------------------+--------------+---------+
| date                | eventype     | counter |
+---------------------+--------------+---------+
| 2016-09-30 12:42:41 | blocked      |       4 |
| 2016-10-03 10:55:55 | blocked      |       1 |
| 2016-10-03 13:43:01 | blocked      |       1 |
| 2016-10-04 15:17:19 | blocked      |       2 |
| 2016-10-04 15:21:03 | blocked      |       3 |

这是CESAR合作伙伴名称收到的总事件类型。

我的SELECT * FROM vpnhistory是:

+----------------------+---------------------+--------------+-------------+
| vpnname              | date                | eventype     | partnername |
+----------------------+---------------------+--------------+-------------+
| treinamento-12345678 | 2016-09-30 12:42:41 | blocked      | CESAR       |
| treinamento-45263748 | 2016-09-30 12:43:53 | unlock       | CESAR       |
| treinamento-92837465 | 2016-09-30 12:46:50 | unlock       | CESAR       |
| treinamento-92837465 | 2016-09-30 12:46:50 | unlock       | CESAR       |
| treinamento-92837465 | 2016-09-30 12:46:50 | unlock       | CESAR       |
+----------------------+---------------------+--------------+-------------+

我需要的例子:

How many eventype treinamento-92837465 exists? (inside partnername CESAR)
How many eventype treinamento-12345678 exists? (inside partnername CESAR)

3 个答案:

答案 0 :(得分:0)

SELECT date, eventtype, count(*)
FROM vpnhistory WHERE partnername   REGEXP 'CESAR'
GROUP BY date, eventtype

这就是你想要的吗?我不明白你的第一个小组条款。

答案 1 :(得分:0)

您需要将用于分组的字段添加到SELECT:

<table>
  <thead>
    <tr>
      <th>Clientes</th>
      <th>January</th>
      <th>February</th>                                                         
    </tr>
</thead>
<tbody>
<?php foreach($stmt as $row)
{   
echo"
<tr>
  <td>".$row['clientid']."</td>
  <td>".$row['january']."</td>
  <td>".$row['february']."</td>                               
</tr>"; }; ?>              
</tbody>
</table>  

答案 2 :(得分:0)

我得到了解决方案:

SELECT vpnname, date, eventype, count(*) AS counter
FROM vpnhistory 
WHERE partnername REGEXP 'CESAR'
GROUP BY (vpnname), (eventype), ((UNIX_TIMESTAMP(date)) DIV 600);

+--------------------------------------------+---------------------+--------------+---------+
| vpnname                                    | date                | eventype     | counter |
+--------------------------------------------+---------------------+--------------+---------+
| vpn-1234                                   | 2016-12-02 08:36:53 | blocked      |       1 |
| vpn-1234                                   | 2016-12-02 08:36:49 | unlocked     |       1 |
| vpn-1823781289371289378192983              | 2016-10-13 16:20:40 | blocked      |       1 |
| vpn-2983494879234789327233                 | 2016-10-19 09:27:13 | blocked      |       1 |
| vpn-2983494879234789327233                 | 2016-10-19 11:56:14 | unblocked    |       1 |
| vpn-9123898293383838                       | 2016-10-03 10:55:55 | blocked      |       1 |