How to split a single row in to multiple columns in mysql

前端 未结 5 1328
轻奢々
轻奢々 2021-01-16 08:50

Simply Asking, Is there any function available in mysql to split single row elements in to multiple columns ? I have a table row with the fields, user_id, user_name, u

5条回答
  •  旧时难觅i
    2021-01-16 09:24

    You should do this in your client application, not on the database.

    When you make a SQL query you must statically specify the columns you want to get, that is, you tell the DB the columns you want in your resultset BEFORE executing it. For instance, if you have a datetime stored, you may do something like select month(birthday), select year(birthday) from ..., so in this case we split the column birthday into 2 other columns, but it is specified in the query what columns we will have.

    In your case, you would have to get exactly that US%UK%JAPAN%CANADA string from the database, and then you split it later in your software, i.e.

    /* get data from database */
    /* ... */
    $user_location = ... /* extract the field from the resultset */
    $user_locations = explode("%", $user_location);
    

提交回复
热议问题