简体   繁体   中英

Break a special character in MySQL

I have a requirement where I need to check a pipe | in the database. If found I need to play around differently.

Here how my db table looks like //Please check the | character in row 11 //Please check the | character in row 11

在此处输入图片说明

And if I run a group by sql command myresult will be

在此处输入图片说明

Which is correct.

But my requirement is to break the | in any cell and give the count accordingly. The expected result as

在此处输入图片说明

Can this be done using MySQL commands alone or do I need to use some php script as well?

Any snippet will be helpful.

Hope this script might help u

$frt =array();
$stmt = $mysqli->prepare("select `fruits` from `meva`") or $mysqli->error ;
$stmt->execute();
$stmt->bind_result($fruits);
 while ($stmt->fetch()) {
    $frt[]=$fruits;
  }

// var_dump($frt); //check all the fruits is in array  
 $res = array();
 $tot = count($frt);
 for($i=0;$i<=$tot;$i++)
  {

    if(preg_match("/\|/", $frt[$i]))
        {
    $res[] =explode( '|', $frt[$i]);
        }else
        {
    $res[] = $frt[$i];
        }
  }

 // var_dump($res);

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($res));
foreach($it as $v) {
  $ary[]=$v;
 }

    $all_fruits = array();
    $tot_ary = count($ary);
  for($io=0;$io<=$tot_ary;$io++)
    {
      if(isset($ary[$io])!='')
    {
     $all_fruits[] = trim($ary[$io]);
    }else
    {
      continue;
    }
    }
 // var_dump($all_fruits);

$newArray = array_count_values($all_fruits);

foreach ($newArray as $key => $value) {
        echo "$key - <strong>$value</strong> <br />"; 
}

You can use php and do it like below

$query = mysql_query("SELECT fruit FROM meva");

$cnt_array   = array();

while($row   = mysql_fetch_assoc($query)){
     $fruits = $row["fruit"];
     $fruit  = explode("|", $fruits);
     foreach($fruit as $fru){

         if(array_key_exists($fru,$cnt_array)){
            $cnt_array[$fru]   = $cnt_array[$fru]+1;
         }
          else{
            $cnt_array[$fru]   = 1;
          }
     }  
    }

print_r($cnt_array);

NOTE : This code is not tested,please try it and edit accordingly

I think you should fix your data. You can run these two statements in a row until all the data is fixed:

INSERT INTO meva (fruits)
SELECT SUBSTR(fruits, LOCATE('|', fruits) - 1)  FROM meva
WHERE LOCATE('|', fruits) > 0;

UPDATE meva
SET fruits = SUBSTR(fruits, LOCATE('|', fruits) + 1)
WHERE LOCATE('|', fruits) > 0;

This will fix the table.

However, if it is your interview question (or a school assignment) just to count from the table as it is, then you can only do it if you know the maximum number of pipes in a given row.

So, if the maximum number of pipes in a row is 1, then your select statement would be:

SELECT count(*),
CASE WHEN LOCATE('|', fruits) > 0 THEN SUBSTR(fruits, LOCATE('|', fruits) - 1) ELSE fruits END
FROM meva
GROUP BY CASE WHEN LOCATE('|', fruits) > 0 THEN SUBSTR(fruits, LOCATE('|', fruits) - 1) ELSE fruits END

If you can have more than one pipe in a row, then your CASE statement will be more complex

Actually the best solution is to change your data structure. This current structure is not recommended. each 'cell' has to contain only one value. If you need to store several fuirts for a specific ID, use

id    fruit

11    Apple
11    Mango

this might require some adjustments to your code / tables, but it will prevent the need for more future hacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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