如何获取my_hit count和more_info的SUM

时间:2014-03-07 21:32:38

标签: php mysql count sum

这是我的桌子。

My Table

这是我到目前为止所做的一切,但是......唯一的问题是我还需要more_info列的总数。我该怎么做呢?

SELECT *, SUM(hit_count) as 'view_count'
    FROM $table_name
    WHERE create_date >='".$fromdate."' 
    AND create_date<='".$todate."'
    AND post_author ='".$_POST['pps_user_id']."'  
    GROUP BY post_id desc

我们得到的是hit_count的总数,但是我也希望得到more_info区域中每个不同的jsn字符串的总数,并且当通过post_id desc分组时也输出

1 个答案:

答案 0 :(得分:0)

不确定这是否回答了您的问题,但您可以使用SUBSTRING_INDEX解析这些值,如下所示

SELECT post_id,jsn_string,SUM(jsn_count) as jsn_count
FROM
(SELECT post_id,
       SUBSTRING_INDEX(SUBSTRING_INDEX(more_info,'"',4),'"',-1) as jsn_string,
       SUBSTRING_INDEX(more_info,':',-3) + 0 as jsn_count
FROM yourtable
UNION All
SELECT post_id,
       SUBSTRING_INDEX(SUBSTRING_INDEX(more_info,'"',8),'"',-1) as jsn_string,
       SUBSTRING_INDEX(more_info,':',-1) + 0 as jsn_count
FROM yourtable
)T1
WHERE jsn_string != ''
GROUP BY post_id,jsn_string

sqlFiddle