Name       Hobby
John    |  Fishing,Traveling
Mary    |  Reading,
Tom     |  Music,
Kate    |  Cooking,Reading
George  |  Traveling,
Peter   |  Fishing,
Lisa    |  Cooking,

我想选这张桌子
Hobby        Count
Fishing   |  2
Traveling |  2
Reading   |  2
Music     |  1
Cooking   |  2

我该怎么做?

最佳答案

我会像“韦斯”一样
如果不可能的话,我会用PHP

$result = array();
$sql = 'SELECT Hobby, count(*) as cnt FROM youttable GROUP BY Hobby';
foreach ($conn->query($sql) as $row) {
    foreach(explode(',',$row['Hobby']) as $hobby) {
        $result[$hobby] += $row['cnt'];
    }
}

关于php - Mysql-如何查询在每个单元格中可能具有多个条目的表并在每个单元格中获得具有单个值的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5668532/

10-13 00:54