Question: [![enter image description here](https://i.stack.imgur.com/RyhBh.png)](https://i.stack.imgur.com/RyhBh.png)

Here i get mos popular hashtags (mos used hashtags) but problem is with comma my query show this:

```
Popular hashtags: 
#q7 (2)
#q9 (2)
#q7 , #q0 (1)
#q0 (1)

```

I want this:

```
Popular hashtags: 
#q7 (3 times )
#q9 (2 times)
#q0 (2 times)

```

my code:

```
$query_result = mysqli_query($Connection, "select hashtag, count(hashtag) as cnt
from tricks
group by hashtag
order by cnt desc limit 5");

$results = array();
while($row = mysqli_fetch_assoc($query_result)) {
   $results[] = "{$row['hashtag']} ({$row['cnt']})";
}
echo "Popular hashtags: </br>", implode("</br>", $results);

```

Answer: 
Php solution:

```
$query_result = mysqli_query($Connection, 
                  "SELECT hashtag FROM tricks");             

$popularHashtags = array();

while($row = mysqli_fetch_assoc($query_result)) {

  $hashtags = explode(',', $row['hashtag']);

  foreach($hashtags as $hashtag) {
      $hashtag = trim($hashtag);

      if (!empty($hashtag)) {

        if (!isset($popularHashtags[$hashtag])) {
          $popularHashtags[$hashtag] = 0;
        }

        $popularHashtags[$hashtag]++;
      } 
  }
}

arsort($popularHashtags);

// for showing popular hashtags

echo "Popular hashtags: <br />";

foreach($popularHashtags as $hashtag => $total) {
  echo $hashtag . " ( " . $total . " ) <br />";
}

```
If you know the number of hastags contained in the field, you can use the [SUBSTRING\_INDEX](http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index) function, or you can see this answer [MySQL split](https://stackoverflow.com/questions/2696884/split-value-from-one-field-to-two#answer-2696901) to split your field.

Something like this (I have not a MySQL instance):

```
SELECT hastag as T1 FROM tricks WHERE hastag NOT LIKE '%,%' -- Take the Hastag without separator in it
UNION
SELECT SUBSTRING_INDEX(hashtag, ',', 1) as T1 FROM tricks WHERE hastag LIKE '%,%' -- Take the first Hastag (before separator)
UNION
SELECT SUBSTRING_INDEX(hashtag, ',', 2) as T1 FROM tricks WHERE hastag LIKE '%,%' -- Take the second Hastag (before separator) 

```