简体   繁体   English

在MySQL中打破特殊字符

[英]Break a special character in MySQL

I have a requirement where I need to check a pipe | 我有一个需要检查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 //Please check the | character in row 11

在此处输入图片说明

And if I run a group by sql command myresult will be 如果我通过sql命令运行组,则myresult将是

在此处输入图片说明

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? 可以单独使用MySQL命令完成此操作,还是需要使用一些php脚本?

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 您可以使用php并按以下方式进行操作

$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: 因此,如果一行中的最大管道数为1,则您的select语句将为:

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 如果您可以连续拥有多个管道,那么您的CASE语句将更加复杂

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存储多个文件,请使用

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. 这可能需要对您的代码/表进行一些调整,但是它将防止将来需要更多的黑客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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