Kodewick
開始學 →
入門←→0 / 70

沒有就寫 none

#046入門COALESCE約 3 分鐘

前臺要印出主人名單,電話一欄空著不好看。從 owners 表裡選出 name 和電話兩列;電話是空(NULL)的,顯示成 'none'。

用到的表
owners
  • idPK
  • name
  • city
  • phone
pets
  • idPK
  • name
  • kind
  • age
  • weight
  • owner_idFK

pets.owner_id → owners.id

owners
idnamecityphone
1LenaOslo555-0101
2MarcoRomeNULL
3PriyaOslo555-0103
4OmarCairoNULL
5SofiaRome555-0105
5 行
pets
idnamekindageweightowner_id
1Biscuitdog3121
2Mochicat541
3Pepperdog8202
4Nemofish1NULL3
5Lunacat233
6Rexdog1130NULL
7Kiwibird4NULL5
8Tofucat75NULL
8 行
query.sql
-- 在這裡寫:把下面的 phone 換成 COALESCE(phone, 'none')(寫在這裡,刪掉下面那條)
SELECT name, phone
FROM owners;
Ctrl + Enter 也能執行
結果

寫好了先點「執行」看看結果,沒問題再點「提交」。