我的 mysql 表如下所示:
id place interest
1 place1 a,b,c
2 place2 c,d,e
3 place1 a,e
4 place2 f
5 place2 f
6 place3 g,h
我需要获得根据计数排序的唯一“地点”和“兴趣”值。 因此,“place”的输出将是
place2(3)
place1(2)
place3(1)
因此,“interest”的输出将是
a(2)
c(2)
e(2)
f(2)
b(1)
d(1)
g(1)
h(1)
有没有办法在 PHP-Mysql 中做到这一点?
到目前为止,我已经能够获取简单的列数据
SELECT place,
COUNT( * ) AS num
FROM testtab
GROUP BY place
ORDER BY COUNT( * ) DESC
最佳答案
由于mysql无法保存数组,所以最好像这样建立一个新表:
interest_id interest_name
1 a
2 b
还有一个来保持关系:
pk id interest_id
1 1 1
2 1 2
这个id是你的主表中记录的id。
有了这个,您就可以轻松使用:
select count(*) from THIRD_TABLE where id = YOUR_ID
关于php - 逗号分隔字段的唯一值计数(PHP - MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17242757/