1

I would like to save user messages to a database. The message will be data object ( class with fields text, sender, date) that contains the message text with meta data:

  • what is the most apperoperiate data type to store object (in this case message).
  • could I append the content of one object (message) to another object without having to read the column value, update t in memory and write it back to disk. this is because I need to store all received message for user until user download them.

thanks.

0

3 Answers 3

4

If there is a chance that the field may contain a null value, the following works:

UPDATE tb_messages 
SET message = CASE 
                WHEN LENGTH(message) > 0 THEN 
                  CONCAT(message, ',', 'New message content')  
                ELSE 'New message content' 
              END
WHERE user_id = 1

Hope it helps :)

3

For updating a field concating new text, you can use the CONCAT() function.

Example:

UPDATE messages SET message = CONCAT(message, 'new text to add') where user_id = 1;

Please note this only works if the value of the existing field (in this example, 'message') is not NULL.

1
  • What a simple answer. Just what I was looking for. Aug 29, 2013 at 9:40
1

text will be type of TEXT
Sender should be sender Id in that case it will be Int or Short Int
and Date can be of type DATE,DATETIME or TIMESTAMP
More info about DATE type goes here.


Your Message will be saved and retrieved in following format

to_id      sender_id     message                       date
22243       22211        blah blah   blah              3/3/2012
22244       22211        blah blah   blah              3/3/2012
22245       22211        blah blah   blah              3/3/2012

I cannot fully understand your second question.
How ever you can append data in your column in mysql using concat
http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.