从wordpress数据库计算相同的值

时间:2015-09-15 01:56:20

标签: php mysql wordpress

我的数据库看起来像这样:

timestamp                user_ip               user_agent
---------                -------               ----------
2015-09-15 01:11:11      111.111.111.111       bla bla Chrome bla bla
2015-09-15 02:22:22      222.222.222.222       bla bla Chrome bla bla
2015-09-15 04:44:44      222.222.222.222       bla bla Chrome bla bla
2015-09-15 05:55:55      222.222.222.222       bla bla Firefox bla bla

我需要计算user_ip raw的相同值。 Timestamp和user_agent仅用于db内的记录。 预期结果:

IP                 Count
---------------    -----
111.111.111.111      1
222.222.222.222      3

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用GROUP BYCOUNT()功能。

SELECT COUNT(1) FROM my_table GROUP BY user_ip;

答案 1 :(得分:0)

试试这个:

 $arr = [
        0 => (object)[
          'timestamp' => '2015-09-15 04:43:40',
          'user_ip' => '104.131.92.125',
          'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
          ],
        1 => (object)[
            'timestamp' => '2015-09-15 04:44:05',
            'user_ip' => '104.131.92.125',
            'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
      ],
        2 => (object)[
            'timestamp' => '2015-09-15 04:44:05',
            'user_ip' => '102.131.92.125',
            'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 )'
        ]
];
    $a = json_encode($arr);
    $b = json_decode($a, true);

    $newArr = [];
    foreach($b as $key => $val)
    {
        if(array_key_exists('user_ip', $val)){
            $newArr[] = $val['user_ip'];
        }

    }
     echo '<pre>';
    $arrValues = array_count_values($newArr);
    print_r($arrValues);

结果如下:

  Array
  (
    [104.131.92.125] => 2
    [102.131.92.125] => 1
  )