方法一
如果你只查一跳 用count(*) +where 就可以了 如果你查寻多条 用 select 字段名 from 表面 where 条件然后 把返回的结果赋值到一个数组里面 用 array_count_values 函数返回你需要的结果 $array = array(1, "hello", 1, "world", "hello"); print_r(array_count_values ($array)); Array ( [1] => 2 [hello] => 2 [world] => 1 )
方法二
比如表base_keywords中有这样的数据:
id apptype keyword count up_time status ip num
1 keyword thinkpad笔记本 10 2010-03-18 15:55:05 2 127.0.0.1 0
2 article thinkpad笔记本 10 2010-03-18 15:55:05 2 127.0.0.1 0
3 product thinkpad笔记本 11 2010-03-18 15:55:05 2 127.0.0.1 0
4 ariticle thinkpad笔记本 12 2010-03-18 15:55:05 1 127.0.0.1 20
现在要查找aptype和keyword对应的数据,当然不能重复,而且要计算count的值,我的解决办法是:
SELECT apptype,keyword,SUM(count) AS count,MAX(up_time) AS up_time FROM base_keywords GROUP BY apptype,keyword
这样得到的结果就是:
keyword thinkpad笔记本 10 2010-03-18 15:55:05
article thinkpad笔记本 22 2010-03-18 15:55:05
product thinkpad笔记本 11 2010-03-18 15:55:05