Can you split/explode a field in a MySQL query?

后端 未结 17 1096
无人及你
无人及你 2020-11-22 04:03

I have to create a report on some student completions. The students each belong to one client. Here are the tables (simplified for this question).

CREATE TAB         


        
17条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 04:38

    Until now, I wanted to keep those comma separated lists in my SQL db - well aware of all warnings!

    I kept thinking that they have benefits over lookup tables (which provide a way to a normalized data base). After some days of refusing, I've seen the light:

    • Using lookup tables is NOT causing more code than those ugly string operations when using comma separated values in one field.
    • The lookup table allows for native number formats and is thus NOT bigger than those csv fields. It is SMALLER though.
    • The involved string operations are slim in high level language code (SQL and PHP), but expensive compared to using arrays of integers.
    • Databases are not meant to be human readable, and it is mostly stupid to try to stick to structures due to their readability / direct editability, as I did.

    In short, there is a reason why there is no native SPLIT() function in MySQL.

提交回复
热议问题