简体   繁体   English

php mysql 选择和计数

[英]php mysql select and count

I have mysql table named 'tagtable' and it has text type column 'params' column like below;我有一个名为 'tagtable' 的 mysql 表,它有如下文本类型列 'params' 列;

id   tag                 params    //in text type 
1   #discount          {"activity":[105,106,107]}
2   #table             {"activity":[108,109,110,111]}
3   #pencil            {"activity":[112,113]}
4   #door              {"activity":[114]}
5   #phone             {"activity":[115]}
6   #wall              {"activity":[116,117,118,119,120]}
7   #radio             {"activity":[121,122,123]}

my expexted result is selecting top 4 tag with most activity number count from text type params column and counting them, like below:我的expexted结果是从文本类型参数列中选择活动数量最多的前4个标签并计算它们,如下所示:

#wall-5  , #table-4 , #discount-3, #radio-3

How can I achieve that with php ?我怎样才能用 php 实现呢?

Try this code试试这个代码

SELECT 
ID, 
tag, 
(CHAR_LENGTH(params) - CHAR_LENGTH(REPLACE(params, ',', '')) + 1) as total 
FROM tagtable 
order by total desc 
limit 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM