String split column and join to another table

后端 未结 3 1776
情话喂你
情话喂你 2021-01-02 02:08

Let\'s say I have 2 tables like this :

Job Offers:

+----+------------+------------+
| ID |    Name    | Categories |
+----+--------         


        
3条回答
  •  盖世英雄少女心
    2021-01-02 02:31

    If you can live with one row per category then this will work:

    select jo.*, c.name as category
    from joboffers jo join
         categories c
         on ',' + jo.categories + ',' like '%,' + cast(c.id) + ',%';
    

    Re-aggregating them into a string is painful in SQL Server (but very possible).

    Note: you have a really, really bad data structure. So you should fix it as mentioned in a comment. Why is it bad?

    • You are storing numbers as strings.
    • You have ids that don't have a foreign key relationship to the reference table.
    • You are storing multiple values in a single field.

提交回复
热议问题