--- tags: sql, LeetCode disqus: HackMD --- # 1527. Patients With a Condition 本題主要考驗 [like](https://www.1keydata.com/tw/sql/sqllike.html) 模糊查詢 `Group Sold Products By The Date` 透過`leetcode 1527`[Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition/)來練習 #### 使用table ![](https://i.imgur.com/7r4JiSU.png) patient_id 是該表的主鍵。 'conditions' 包含 0 個或多個以空格分隔的代碼。 此表包含醫院患者的信息。 ## 題目說明: 編寫 SQL 查詢以報告patient_id,patient_name I 型糖尿病患者的所有狀況。I型糖尿病總是以**DIAB1**前綴開頭 按任意順序返回結果表。 查詢結果格式如以下範例所示。 ![](https://i.imgur.com/VwvhBoI.png) ## 解題: 1. `conditions like 'DIAB1%'` 判斷資料是否為DIAB1開頭 2. `conditions like '% DIAB1%'`判斷資料是否為xxx DIAB1 一開始以為只要使用 `conditions like '%DIAB1%'` 即可 後來發現可能出現 xxxDIAB1xxx 類型的資料 故改寫成以下寫法 ```sql= select patient_id,patient_name,conditions from Patients where conditions like 'DIAB1%' or conditions like '% DIAB1%' ```