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

后端 未结 17 1201
无人及你
无人及你 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:37

    I just had a similar issue with a field like that which I solved a different way. My use case was needing to take those ids in a comma separated list for use in a join.

    I was able to solve it using a like, but it was made easier because in addition to the comma delimiter the ids were also quoted like so:

    keys "1","2","6","12"

    Because of that, I was able to do a LIKE

    SELECT twwf.id, jtwi.id joined_id FROM table_with_weird_field twwf INNER JOIN join_table_with_ids jtwi ON twwf.delimited_field LIKE CONCAT("%\"", jtwi.id, "\"%")

    This basically just looks to see if the id from the table you're trying to join appears in the set and at that point you can join on it easily enough and return your records. You could also just create a view from something like this.

    It worked well for my use case where I was dealing with a Wordpress plugin that managed relations in the way described. The quotes really help though because otherwise you run the risk of partial matches (aka - id 1 within 18, etc).

提交回复
热议问题