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?
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 :
You could use delete
command to delete it:
DELETE FROM table_name
WHERE some_column=some_value
$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("
", $arr[1]);
$key = array_search($searchQuery, $system);
if ($key !== false)
unset($system[$key]);
$system = implode("
", $system);
mysql_query("UPDATE `System_t` SET `System`='" . $system . "' WHERE `id`='" . $arr[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 :).
Update system_t SET system=REPLACE(system,'event','') WHERE system REGEXP '[[:<:]]event[[:>:]]';
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.