将包含数组的SQL字段拆分为新的表/行

时间:2015-09-04 18:04:03

标签: mysql sql arrays join string-matching

我需要一个当前存储在较大表格的单个字段中的用户ID列表(course_user_id s。)

我有一个名为courses的表格,其中包含course_idcourse_students的课程信息:

-----------------------------------------------------------
| course_id | course_students                             |
----------------------------------------------------------
| 1         | a:3:{i:0;i:12345;i:1;i:22345;i:2;i:323456;} |
-----------------------------------------------------------
| 2         | a:32:{ … }                                  |
-----------------------------------------------------------

course_students部分包含3个信息块:

  1. 学生人数(a: 3 :{...) - 不需要
  2. 每个学生的数组的顺序/键({i: 0 ; ... i: 1 ; ... i: 2 ; ......}) - 也不需要
  3. course_user_id(i: 12345 ; ...我: 22345 ; ...我: 32345 ;)
  4. 我只需要course_user_id和原始course_id,从而产生一个新表,我可以用于这样的连接/子查询:

    ------------------------------
    | course_id | course_user_id |
    ------------------------------
    | 1         | 12345          |
    ------------------------------
    | 1         | 22345          |
    ------------------------------
    | 1         | 323456         |
    ------------------------------
    

    (理想情况下,能够继续突破其他course_idcourse_user_id的值,但不是优先级:)

    | …         | …              |
    ------------------------------
    | 2         | …              |
    ------------------------------
    | 2         | …              |
    ------------------------------
    | 97        | …              |
    ------------------------------
    | 97        | …              |
    ------------------------------
    | …         | …              |
    ------------------------------
    

    注意:course_user_id的长度可能不同(有些是5位数,有些是6位)

    任何想法都会非常感激!

    更新

    我的用户表格中有user_id,可以映射到course_studentscourse_user_id,因此从下面可以看出非常有用的观察结果。

    我还认为我需要使用LEFT JOIN,因为有些学生在多个课程中注册,我希望看到每个实例/组合。

2 个答案:

答案 0 :(得分:1)

我们假设您有一个表名users,其中包含所有用户数据以及user_id

现在,您可以按以下方式加入表格courses和表格users

select c.course_id,u.user_id

from
courses c
join users u
on u.user_id=if(instr(c.course_students,concat(":",u.user_id,";"))>0,u.user_id,c.course_students)

您可以根据自己的要求获得结果。 在http://sqlfiddle.com/#!9/3667d/2

处验证

注意:如果user_idarray index之间没有重叠,则上述查询可以正常工作。如果重叠,请使用where-clause

过滤数据

答案 1 :(得分:0)

如果我的目标正确,你就有14:20 Executing in H:\0000ybzhao\Programming\00.git\pairSam2Bed: C:\Program Files\Git\bin\git.exe push warning: push.default is unset; its implicit value has changed in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the traditional behavior, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name. Since Git 2.0, Git defaults to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch. See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. The command "C:\Program Files\Git\bin\git.exe" terminated with exit code 128. 表。 users等于{i:0;i:12345;i:1;i:22345;i:2;i:323456;users.id=12345等。

如果我的猜测是正确的,你可以尝试这个解决方案:

http://sqlfiddle.com/#!9/cfef27/5

users.id=22345