1

I've problematic data in some row of my talbe like following string

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32'] 

Now i want to replace them by only one same string like:

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

Please keep in mind that the value of the attr is varying.

-- update --

how its stored in database the column name is called content and the value in this column is

the text of the article
 [data.... ]
 [data.... ]
 [data.... ]

the datatype of the content is longtext

Sorry i can't make the proper structure of sql table.

Thanks in advance for your help.

7
  • how is they stored in the database? just a text field? Commented Jul 31, 2012 at 11:35
  • Check this out : stackoverflow.com/questions/1651999/… Commented Jul 31, 2012 at 11:36
  • plz provide your table structure Commented Jul 31, 2012 at 11:37
  • is all these duplicate strings are separated by "\n" ? Commented Jul 31, 2012 at 11:40
  • @user868766 No there is no "\n" Commented Jul 31, 2012 at 11:51

3 Answers 3

2

You can use DISTINCT or GROUP BY to remove duplicate values, depending on other needs. For example:

select distinct mycolumn from mytable;

Or:

select mycolumn from mytable group by mycolumn;
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to remove/delete duplicate rows you can use

DELETE t 
FROM t 
LEFT JOIN t AS t2 ON (t.content = t2.content AND t.id > t2.id) 
WHERE t2.id;

Before:

SELECT * FROM t;
+----+---------+
| id | content |
+----+---------+
|  1 | abc     |
|  5 | abc123  |
|  9 | abc123  |
| 13 | abc     |
| 17 | abc     |
+----+---------+

After:

SELECT * FROM t;
+----+---------+
| id | content |
+----+---------+
|  1 | abc     |
|  5 | abc123  |
+----+---------+

Comments

0

Here is the script that made my work.

$link = mysqli_connect("SERVER", "root", "root");
    $db = mysqli_select_db($link, "test");
    $res = mysqli_query($link, "SELECT * FROM wp_posts WHERE post_content LIKE '%nutr-label servingsize%nutr-label servingsize%'");

    if($res){
        $i=1;
        while($row = mysqli_fetch_array($res)){
            $str1 = strrpos($row['post_content'],"[nutr-label servingsize='");
            $sub1 = substr($row['post_content'],$str1);
            $str2 = strrpos($row['post_content'],"']");

            $finalStr = substr($row['post_content'],$str1,$str2);
            $newPostContent = substr($row['post_content'], 0, strpos($row['post_content'],"[nutr-label servingsize='")).$finalStr;
            $up = mysqli_query($link,"UPDATE wp_posts SET post_content ='".addslashes($newPostContent)."' WHERE ID='".$row['ID']."'");
            $i++;
        }
    }else{
        echo mysqli_error();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.