1527. Patients With a Condition

本題主要考驗 like 模糊查詢

Group Sold Products By The Date

透過leetcode 1527Patients With a Condition來練習

使用table

patient_id 是該表的主鍵。
‘conditions’ 包含 0 個或多個以空格分隔的代碼。
此表包含醫院患者的信息。

題目說明:

編寫 SQL 查詢以報告patient_id,patient_name I 型糖尿病患者的所有狀況。I型糖尿病總是以DIAB1前綴開頭

按任意順序返回結果表。

查詢結果格式如以下範例所示。

解題:

  1. conditions like 'DIAB1%' 判斷資料是否為DIAB1開頭
  2. conditions like '% DIAB1%'判斷資料是否為xxx DIAB1

一開始以為只要使用 conditions like '%DIAB1%' 即可
後來發現可能出現 xxxDIAB1xxx 類型的資料 故改寫成以下寫法

select patient_id,patient_name,conditions
from Patients
where conditions like 'DIAB1%'
or conditions like '% DIAB1%'