Pages

Thursday, March 13, 2014

MySQL: Split comma separated list into multiple rows

    In MySQL this can be achieved as below  
 
    SELECT id, length FROM vehicles WHERE id IN ( 117, 148, 126)

    +---------------+
    | id  | length  |
    +---------------+
    | 117 | 25        |
    | 126 | 8        |
    | 148 | 10        |
    +---------------+

    SELECT id,vehicle_ids FROM load_plan_configs WHERE load_plan_configs.id =42

    +---------------------+
    | id  | vehicle_ids   |
    +---------------------+
    | 42  | 117, 148, 126 |
    +---------------------+

    Now to get the length of comma separated vehicle_ids use below query

    Output

    SELECT length FROM vehicles, load_plan_configs   WHERE load_plan_configs.id = 42 AND FIND_IN_SET(vehicles.id, load_plan_configs.vehicle_ids)
   
    +---------+
    | length  |
    +---------+
    | 25        |
    | 8          |
    | 10       |
    +---------+

3 comments:

  1. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    ReplyDelete
  2. http://amitbrothers.blogspot.com/2014/03/mysql-split-comma-separated-list-into.html
    https://gyanbest.com/2019/03/instant-approval-blog-commenting-sites-list.html
    http://foxnangel.blogspot.com/2018/02/a-guide-to-digital-marketing-in-todays.html
    https://www.shoutonme.xyz/2017/01/blogger-seo-tips-to-take-your-blogspot.html
    http://empowernetwork.atlcomputerdude.com/2015/06/book-luxury-private-jet-charters.html

    ReplyDelete
  3. Got here from https://newbedev.com/mysql-split-comma-separated-list-into-multiple-rows
    This solution really helped me

    ReplyDelete