English 中文(简体)
从数据库中删除 com离职清单中的数值
原标题:Remove values in comma separated list from database

我的MySQL数据库中有一个叫孩子的表格。 该表是一份称为“愿望”的段落(儿童希望列入清单的 com名单)。 我需要能够更新这一清单,以便它只能消除一个价值。 i.e. 名单=12名普通jeans,Surf板,Red Sox Baseball Cap;我想去掉Surf板。

我现在要问一下。

$select = mysql_query( SELECT * FROM children WHERE caseNumber=" .$caseNum. " LIMIT 1  );
    $row = mysql_fetch_array($select);

    foreach ($wish as $w) {
        $allWishes = $row[ wishes ];
        $newWishes = str_replace($w,   , $allWishes);
        $update = mysql_query("UPDATE children SET wishes= $newWishes  WHERE caseNum= ".$caseNum." ");
}

但是,人民民主党的询问并没有消除任何东西。 我怎么能做我所需要的呢?

最佳回答

可通过网站https://treaties.net/mysql-udf-regexp"rel=“noestlow”查询用户定义的“ < REGEXP_REP LatinE( ) 功能>:>

UPDATE children SET wishes = REGEXP_REPLACE(wishes,  (,(s)?)?Surfboard ,   ) WHERE caseNum= whatever ;

不幸的是,你不能仅仅使用老式REPLACE(),因为你不知道在座标上哪里出现。 事实上,如果Surf板在开端或终点,上述reg可能还需要额外的 t。

也许你可以把铅和拖车 com等地拖走:

UPDATE children SET wishes = TRIM(BOTH  ,  FROM REGEXP_REPLACE(wishes,  (,(s)?)?Surfboard ,   )) WHERE caseNum= whatever ;

因此,这里的情况如何? 该法规取消了Surf板加选择性 com子和amp子;摆在它面前的空间。 然后,围绕<代码>TRIM()的功能消除了在护堤开始时可能出现的主要 com。 这或许也可以由监管机构处理,但坦率地说,我太急于回避。

Note, I ve never used these myself and cannot vouch for their effectiveness or robustness, but it is a place to start. And, as others are mentioning in the comments, you really should have these in a normalized wishlist table, rather than as a comma-separated string.

<>Update>

想更多的话,我更部分地只是强迫使用载于REPLACE()的外 com,然后清除你可能在一行中带两mas子的外 com。 这正同时寻找两个mas子,因为没有把你原来的清单项目分开的空间。 如果这些物品按mas马和空间分开,则更改<代码>,至,,在外部<代码>上贴上>。

UPDATE children SET wishes = TRIM(BOTH  ,  FROM REPLACE(REPLACE(wishes,  Surfboard ,   ),  ,, ,  , )) WHERE caseNum= whatever ;
问题回答

并不是对你的问题的直接回答,但正如达伦一样,它更希望自己提出。 也许你可以改变你的数据库图象,这样你有3个表格,例如:

children
-> caseNum
-> childName

wishes
-> caseNum
-> wishId
-> wishName

childrensWishes
-> caseNum
-> wishId

然后添加或删除儿童的愿望,请在<子女/代码>上添加或删除相关词语。 你目前的设计使得难以操纵(正如你再次发现的那样),使你面临数据不一致的风险。

作为更直接的答案,你可以确定目前的方式,把希望清单看上去,删除你不希望从阵列中去的东西,把它重新 to到更新数据库。

希望表采用这一格式:

caseNumber,wish

之后,你们会得到所有儿童的愿望:

SELECT * FROM children c left join wishes w on c.caseNumber = w.caseNumber WHERE c.caseNumber= ?

放弃希望:

DELETE from wishes where caseNumber = ?

添加希望:

INSERT into wishes (caseNumber,wish) values (?,?)

一种愿望是:

SELECT * FROM children c left join wishes w on c.caseNumber = w.caseNumber WHERE c.caseNumber= ? LIMIT 1

如果把希望指数化成一个阵列,那么就是一个想法,否则,你将需要收回地块,把地块塞起来,去除你不想要的那部分地块,然后将遗骸 con。 可以通过使用explode(功能来实现。

如果你要使用一个阵列,你将检索阵列,然后通过该阵列进行 like:

// Wishes array:
// Array (
//      [0] Regular Jeans
//      [1] Surfboard
//      [2] Red Sox Baseball Cap
// )

$wishes = $row[ wishes ]; // This is a serialized array taken from the database
$wishes = unserialize($wishes);

foreach ($wishes as $key => $value) {
    if ($value ==  Surfboard ) {
        unset($wishes[$key]);
        break;
    }
}

$wishes = serialize($wishes);
// Update database

铭记指数 *** 目前已在阵列中获胜,因此,如果你希望有一个干净的阵列,你就应当穿透阵列,并自己制造一个新的阵列:

foreach ($wishes as $wishes) {
    $newArray[] = $wishes;
}

I think the best answer to such issue is here The best way to remove value from SET field?

问题应当如此,既包括 com失散的一栏中的价值、价值或价值,也只包括价值。

UPDATE yourtable
SET
  categories =
    TRIM(BOTH  ,  FROM
      REPLACE(
        REPLACE(CONCAT( , ,REPLACE(col,  , ,  ,, ),  , ), ,2, ,   ),  ,, ,  , )
    )
WHERE
  FIND_IN_SET( 2 , categories)

在这里,你可以在条款中规定条件。 详情见上文链接。

你可以发挥这样的作用:

CREATE  FUNCTION `remove_from_set`(v int,lst longtext) RETURNS longtext CHARSET utf8
BEGIN
set @lst=REPLACE(@lst,  ,, ,  , );
set @lng=LENGTH(@lst) - LENGTH(REPLACE(@lst,  , ,   ))+1;
set @p=find_in_set(@v,@lst);
set @l=SUBSTRING_INDEX( @lst,  , , @p-1);
set @r=SUBSTRING_INDEX( @lst,  , , @p-@lng);
IF @l!=   AND @r!=   THEN
    return CONCAT(@l, , ,@r);
ELSE
    RETURN CONCAT(@l,  ,@r);
END IF;
END

使用:

SELECT remove_from_set( 1,,2,3,4,5,6 ,1)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签