简体   繁体   English

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

[英]SQL query that searches comma-delimited field

I have a student table which looks something like this:我有一个学生表,看起来像这样:

 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  

The school_descriptors field is just one column, and stores information about the Course, Qualification and Graduation year as a comma-delimited string. school_descriptors 字段只是一列,将有关课程、资格和毕业年份的信息存储为逗号分隔的字符串。 (it's terribly designed and I wish it could be split up into its own fields, but it can't right now (I am not the database owner)) (它的设计非常糟糕,我希望它可以分成自己的领域,但现在不能(我不是数据库所有者))

I want to provide an interface where teachers can quickly find students that match certain Course, Qualifications and Graduation years, and thus would like to create relevant queries.我想提供一个界面,让教师可以快速找到与特定课程、资格和毕业年份相匹配的学生,从而创建相关查询。

Question 1: For example, I would like a teacher to be able to select from the UI: "Business", "MBA" and get returned students with ID 2 and 3. Specifically, an example question I have is: Find students who are in the Business Course and doing the MBA qualification:问题 1:例如,我希望老师能够从 UI 中选择:“商科”、“MBA”并获得 ID 为 2 和 3 的回国学生。具体来说,我有一个示例问题:查找在商业课程中并获得 MBA 资格:

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

The query I have in mind is a basic LIKE query, but I can't help but think there is a more efficient query that can take advantage of the fact that the school_descriptor string is 1) always in a specific order (eg course, qualification, graduation), and 2) comma-delimited, and thus could be perhaps split.我想到的查询是一个基本的 LIKE 查询,但我不禁想到有一个更有效的查询,可以利用 school_descriptor 字符串为 1) 始终按特定顺序(例如课程、资格)的事实, 毕业), 和 2) 逗号分隔,因此可能会被拆分。 The table currently sits at ~5000 rows so relatively small but is expected to grow.该表目前有大约 5000 行,因此相对较小,但预计会增长。

Related question 2 : Find students who are in the Comp Sci Course and graduating after 2019:相关问题 2查找 Comp Sci 课程中 2019 年后毕业的学生:

Would it be possible to split the school_descriptors field and add a >2019 operand?是否可以拆分 school_descriptors 字段并添加>2019操作数?

Many thanks!非常感谢!

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;

output:输出:

+------+-------+----------+----------------+------+
| 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 |
+------+-------+----------+----------------+------+

A link to the docs, in case you want to know about SUBSTRING_INDEX()文档链接,以防您想了解SUBSTRING_INDEX()

In MySql you can use the function SUBSTRING_INDEX() to split the column school_descriptors .在 MySql 中,您可以使用函数SUBSTRING_INDEX()来拆分列school_descriptors
This will work only if the positions of Course, Qualification and Graduation year are fixed.这仅在课程、资格和毕业年份的位置固定时才有效。

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 

See the demo .请参阅演示
Results:结果:

> 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    

Answer 1:答案 1:

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

By using this query you can get all the records that are having Business OR MBA.通过使用此查询,您可以获得拥有 Business OR MBA 的所有记录。 If you want to select only Business, MBA you can try like this如果你只想选择Business, MBA你可以这样试试

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

Answer 2:答案 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM