搜索逗号分隔字段的 SQL 查询

问题描述 投票:0回答:4

我有一张学生桌,看起来像这样:

 id  |  name  |  school_descriptors
-------------------------------------------------------
 1   |  Rob   |  Comp Sci,Undergraduate,2020  
 2   |  Tim   |  Business,MBA,2022
 3   |  Matt  |  Business,MBA,2022
 4   |  Jack  |  Law,Masters,2024
 5   |  Steph |  Comp Sci,Masters,2022  

school_descriptors 字段只是一列,并将有关课程、资格和毕业年份的信息存储为逗号分隔的字符串。 (它的设计非常糟糕,我希望它可以分成自己的字段,但现在不能(我不是数据库所有者))

我想提供一个界面,教师可以快速找到与某些课程、资格和毕业年份相匹配的学生,从而创建相关查询。

问题 1: 例如,我希望老师能够从 UI 中选择:“商业”、“MBA”并返回 ID 为 2 和 3 的学生。具体来说,我的示例问题是: 查找正在学习商业课程并获得 MBA 资格的学生:

SELECT * FROM student_table WHERE school_descriptors LIKE '%Business%' AND school_descriptors LIKE '%MBA%'

我想到的查询是一个基本的 LIKE 查询,但我忍不住认为有一个更有效的查询,可以利用 school_descriptor 字符串是 1) 始终按特定顺序的事实(例如,课程,资格、毕业)和 2)以逗号分隔,因此可能会被拆分。该表目前约有 5000 行,相对较小,但预计会增长。

相关问题2查找计算机科学课程并在2019年后毕业的学生:

是否可以拆分 school_descriptors 字段并添加

>2019
操作数?

非常感谢!

mysql sql split
4个回答
1
投票

在MySql中,您可以使用函数

SUBSTRING_INDEX()
来拆分列
school_descriptors

仅当课程、资格和毕业年份的位置固定时,这才有效。

select *,
  substring_index(school_descriptors, ',', 1) Course, 
  substring_index(substring_index(school_descriptors, ',', 2), ',', -1) Qualification, 
  substring_index(school_descriptors, ',', -1) Graduation
from student_table 

查看演示
结果:

> id | name  | school_descriptors          | Course   | Qualification | Graduation
> -: | :---- | :-------------------------- | :------- | :------------ | :---------
>  1 | Rob   | Comp Sci,Undergraduate,2020 | Comp Sci | Undergraduate | 2020      
>  2 | Tim   | Business,MBA,2022           | Business | MBA           | 2022      
>  3 | Matt  | Business,MBA,2022           | Business | MBA           | 2022      
>  4 | Jack  | Law,Masters,2024            | Law      | Masters       | 2024      
>  5 | Steph | Comp Sci,Masters,2022       | Comp Sci | Masters       | 2022    

1
投票
select id, name, 
  substring_index(school_descriptors,',',1) as course, 
  substring_index(substring(school_descriptors,length(substring_index(school_descriptors,',',1))+2,200),',',1) as Qualifications, 
  substring_index(school_descriptors,',',-1) as year
from student;

输出:

+------+-------+----------+----------------+------+
| id   | name  | course   | Qualifications | year |
+------+-------+----------+----------------+------+
|    1 | Rob   | Comp Sci | Undergraduate  | 2020 |
|    2 | Tim   | Business | MBA            | 2022 |
|    3 | Matt  | Business | MBA            | 2022 |
|    4 | Jack  | Law      | Masters        | 2024 |
|    5 | Steph | Comp Sci | Masters        | 2022 |
+------+-------+----------+----------------+------+

文档链接,以防您想了解 SUBSTRING_INDEX()


1
投票

答案1:

SELECT * FROM student_table WHERE school_descriptors REGEXP ['Business','MBA']

通过使用此查询,您可以获得包含 Business 或 MBA 的所有记录。 如果你只想选择商科、MBA你可以这样尝试

从学生表中选择*,其中学校_描述符如“%Business,MBA%”

答案2:

SELECT * 
FROM student 
WHERE
  SUBSTRING_INDEX(SUBSTRING_INDEX(school_descriptors , ',', 1), ',', -1)='Comp Sci' 
  AND
  SUBSTRING_INDEX(SUBSTRING_INDEX(school_descriptors , ',', 3), ',', -1)> 2019;

0
投票

LIKE 运算符不是最佳选择。在某些情况下,您的要求会失败。例如,如果您在学校描述符列中有主业务,则 LIKE 运算符也将提取该记录。要找到完全匹配的内容,您需要使用 RLIKE

从学生表中选择 *,其中 school_descriptors RLIKE '[[:<:]]'Business'[[:>:]]' AND school_descriptors RLIKE '[[:<:]]'MBA'[[:>:]]'

© www.soinside.com 2019 - 2024. All rights reserved.