0

My table structure in the database;

Table "System_t"

id         |    Owner     |    System
===========================================
1          |    Joe       |    Registration
           |              |    Event             //contains new line
           |              |    Aims
-------------------------------------------
2          |    Rose      |    Ticket
           |              |    Elearning

NOTE : The data field "System" were saved from a Textarea box.

My problem is how am I going to delete only the "Event" data from the field "System"?

My table should be look like this when it is deleted;

id         |    Owner     |    System
===========================================
1          |    Joe       |    Registration
           |              |    Aims
-------------------------------------------
2          |    Rose      |    Ticket
           |              |    Elearning 

Any idea how to do it without changing the table structure?

Hafiz Abdullah
  • 248
  • 1
  • 5
  • 14

6 Answers6

5

An UPDATE statement with a REPLACE should be better in this case.

Look at this post, it might help you understand what I'm talking about :

Deleting part of a string in MYSQL

Community
  • 1
  • 1
BMN
  • 8,253
  • 14
  • 48
  • 80
  • Actually, this is better than my regex answer since, well, regex is unnecessary for a fixed string :-) + 1 and deleting mine. – paxdiablo Jun 15 '12 at 09:19
  • 1
    Yes, I though the REGEX solution was a bit complex for this kind of trick. But I have to add that, even if the OP doesn't want to talk about it, the structure of the table should be reviewed. – BMN Jun 15 '12 at 09:20
  • I still think you should be using like. On large tables what tsabz proposed won't be using any index. – mihaisimi Jun 15 '12 at 09:41
  • What I'm proposing is just a way of doing the trick. He's free to modify the request adding any kind of `like` clause or whatever he wants. – BMN Jun 15 '12 at 09:45
0

You could use delete command to delete it:

DELETE FROM table_name
WHERE some_column=some_value
TRR
  • 1,637
  • 2
  • 26
  • 43
Jathin
  • 79
  • 1
  • 1
  • 8
0
$searchQuery = "Event";

$query = mysql_query("SELECT `id`, `System` FROM `System_t` WHERE `System` LIKE '%" . $searchQuery . "%'");

if (mysql_num_rows($query) > 0) {
    while ($arr = mysql_fetch_row($query)) {
        $system = explode("\n", $arr[1]);
        $key = array_search($searchQuery, $system);
        if ($key !== false)
            unset($system[$key]);

        $system = implode("\n", $system);

        mysql_query("UPDATE `System_t` SET `System`='" . $system . "' WHERE `id`='" . $arr[0] . "'");
    }
}
Ananth
  • 4,227
  • 2
  • 20
  • 26
0

Try this:

 update System_t su
       set su.System = replace(su.System, "Event", "") 
        where su.System like "%Event%";

I haven't taken the new lines into account I leave this for you as an easy exercise :).

mihaisimi
  • 1,911
  • 13
  • 15
0
Update system_t SET system=REPLACE(system,'event','') WHERE system REGEXP '[[:<:]]event[[:>:]]';
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
sel
  • 4,982
  • 1
  • 16
  • 22
0

For PostgreSQL, you coud use something like (because I believe there's no direct replace function as in MySQL):

UPDATE "System_t"
SET "System" = overlay("System" placing '' 
               from (position('Events' in "System")) 
               for 6)
WHERE "System" LIKE '%Events%'

You can see the description of the overlay and position functions here: http://www.postgresql.org/docs/8.0/static/functions-string.html

And 6 is the size of the string you are removing.

Anyway, I would recommend for you to create another table with, for example, (id, system_id, system_type), with system_id being a foreign key poiting to the other table. That way, you would have a better model and avoid a lot of inconsistency.

Álvaro
  • 269
  • 1
  • 9