0

I have a table Template. In that I have one column like Message.For example

-------------------------------
id  name  desc cretaedBy  Message
-------------------------------
1    A     blah  today      <td><tr>one</tr></td></body></html>
2    B     blah  lastday
3    C     blah  onemonth   <tr><td>last</td><td>next</td></tr></body></html> 

Here, i need to insert <div>blah blah some content</div> before </body></html> in Message-column.

How to find the position and update in particular position using update query.

This table shows 3records for example only..but it may contain1000records. Actually what i am expecting, for example if java or any programming language...if string contains end body tag , substring the string and add data before end body tag.

Anyone please help me in mysql

Learner
  • 43
  • 1
  • 1
  • 8

3 Answers3

0

You can use an update statement:

UPDATE Template SET Message = '<tr><td>last</td><td>next</td></tr><div>blah blah some content</div></body></html>' 
WHERE Message = '<tr><td>last</td><td>next</td></tr></body></html>';
Willem
  • 992
  • 6
  • 13
  • No. you are using constant value in where condition.Some cases ,records wont contain no data in Message . i am asking if footer contains

    , I have to insert 'data' before

    – Learner Jan 03 '20 at 17:35
  • Please put that information into your question. So you have to insert data before

    but sometimes it's empty so what do you want then?

    – Willem Jan 03 '20 at 17:38
  • Hi willem thanks.i have updated my question . 1000 records already there...if message column contains

    , i need to add some data before ...so if some records contains no data in message column , i should not update corresponding the record..

    – Learner Jan 03 '20 at 19:31
0

Try this: Create template DDL statement:

CREATE TABLE `mytable` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) unsigned DEFAULT NULL COMMENT 'a name comment',
  `Message` varchar(5000) DEFAULT NULL COMMENT 'an address comment',
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`))

Create template INSERT data statement:

Insert into mytable (name,message) VALUES('A','<td><tr>one</tr></td>?</body></html>')
...
Insert into mytable (name,message) VALUES('C','<td><tr>last</tr></td>?</body></html>')

Use Prepare. Your data will be inserted into the SQL query template at the question mark place.

    mysql> PREPARE stmt1 FROM 'Insert into mytable (name,message) VALUES('A','<td><tr>one</tr></td>?</body></html>');
Insert into mytable (name,message) VALUES('C','<td><tr>last</tr></td>?</body></html>');';
mysql> SET @a = '<div>blah blah some content</div>';
mysql> EXECUTE stmt1 USING @a;
mysql> DEALLOCATE PREPARE stmt1;

Create and fill in the table with the data whenever necessary.

o_nerti
  • 11
  • 7
  • Hi thanks.but i need to write update script...bcoz already records there..i want to update wherever message column contains – Learner Jan 03 '20 at 19:27
  • then export the whole table along with its contents to a sql file and perform the replacement in a text editor. Then drop your table and run the script. – o_nerti Jan 03 '20 at 22:43
0

You can use an UPDATE with a REPLACE, where you replace </body></html> with <div>blah blah some content</div></body></html>. In Message values which don't have </body></html> in them, nothing will happen:

UPDATE Template
SET Message = REPLACE(Message, '</body></html>', 'div>blah blah some content</div></body></html>')
;
SELECT * FROM Template

Output:

id  name    desc    createdBy   Message
1   A       blah    today       <td><tr>one</tr></td>div>blah blah some content</div></body></html>
2   B       blah    lastday     (null)
3   C       blah    onemonth    <tr><td>last</td><td>next</td></tr>div>blah blah some content</div></body></html>

Demo on SQLFiddle

If your table is very big, qualifying the UPDATE with WHERE Message LIKE '%</body></html>' will speed it up.

Nick
  • 138,499
  • 22
  • 57
  • 95