Serialize vs Implode

前端 未结 4 1572
执念已碎
执念已碎 2021-01-15 11:33

What do you think is the better way to go about storing a few image id\'s inside a record in a MySQL database? It\'s just the image id\'s which will be used to fetch the ima

4条回答
  •  臣服心动
    2021-01-15 12:13

    If you don't want to (over?)normalize your tables, and you really just want to store a list of ids then I suggest using a simple comma-separated list, because already MySQL has some functions which can directly deal with comma-separated string values:

    FIND_IN_SET: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

    SELECT FIND_IN_SET('b','a,b,c,d'); --> 2
    

    CONCAT_WS: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws

    SELECT CONCAT_WS(',','First name',NULL,'Last Name'); --> 'First name,Last Name'
    

    Of course, you won't be able to do SQL JOINs, but it still can be helpful.

提交回复
热议问题