开发者

How to insert strings with commas in MySQL?

I have a MySQL field called user_tags, and I want to insert a string with commas like this:

  1. string1
  2. string1,string2
  3. string1,string2,string3,......

How do I upda开发者_开发知识库te the field dynamically using a query?


Please read about Mysql set DataType http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html

But this is the concept:

UPDATE set_test SET myset = CONCAT(myset,",Travel")
      WHERE rowid = 3;


No you don't, that violates first normal form which says "Every row-and-column intersection contains exactly one value from the applicable domain". Also, it's not the right way of doing it; it's not how SQL databases are designed to work.

MySQL's sets don't solve it either (they also violate 1NF) because the set permitted values are effectively fixed (can only be changed by ALTER TABLE).

What you really want, is another table associating tags with users. It's dead easy.

Once you redesign your table this way, you can add a new tag with a simple INSERT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜