LeetCode数据库_1527.患某种疾病的患者

发布时间:2026/8/1 14:07:25
LeetCode数据库_1527.患某种疾病的患者 一、题目患者信息表Patients----------------------- | Column Name | Type | ----------------------- | patient_id | int | | patient_name | varchar | | conditions | varchar | ----------------------- 在 SQL 中patient_id 患者 ID是该表的主键。 conditions 疾病包含 0 个或以上的疾病代码以空格分隔。 这个表包含医院中患者的信息。查询患有 I 类糖尿病的患者 ID patient_id、患者姓名patient_name以及其患有的所有疾病代码conditions。I 类糖尿病的代码总是包含前缀DIAB1。按任意顺序返回结果表。查询结果格式如下示例所示。示例 1:输入Patients表---------------------------------------- | patient_id | patient_name | conditions | ---------------------------------------- | 1 | Daniel | YFEV COUGH | | 2 | Alice | | | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | | 5 | Alain | DIAB201 | ----------------------------------------输出---------------------------------------- | patient_id | patient_name | conditions | ---------------------------------------- | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | ----------------------------------------解释Bob 和 George 都患有代码以 DIAB1 开头的疾病。二、解答需求找出conditions字段中存在以 DIAB1 开头的独立疾病代码字段内多个疾病用空格隔开。 合法匹配场景字段开头就是 DIAB1xxxDIAB100 MYOP中间包含 DIAB1xxxACNE DIAB100字段只有 DIAB1xxxDIAB201非法场景XDIAB123前缀不是 DIAB1直接拼接在其他代码后正则规则拆解需要匹配三种情况字符串开头^DIAB1前面带空格DIAB1用正则表示(^| )DIAB1^匹配字符串开头|或匹配空格方案 1MySQL8.0 REGEXP_LIKE推荐SELECT patient_id, patient_name, conditions FROM Patients WHERE REGEXP_LIKE(conditions, (^| )DIAB1);正则说明(^| )DIAB1(^| )分组匹配行首或者空格DIAB1固定前缀保证疾病代码以 DIAB1 起始方案 2兼容 MySQL5.7 REGEXPSELECT patient_id, patient_name, conditions FROM Patients WHERE conditions REGEXP (^| )DIAB1;方案 3不用正则普通 LIKE备选也可以不用正则多条件拼接实现SELECT patient_id, patient_name, conditions FROM Patients WHERE conditions LIKE DIAB1% OR conditions LIKE % DIAB1%;