建立互動式網站(如求職網、動態內容)需儲存訪客資訊、檢索符合條件的資料。資料庫是核心基礎設施。
本書涵蓋 ASP 開發所需核心 SQL,完整細節請參考 SQL Server 專書。
表由「欄」與「列」組成:
| Name | Email Address |
|---|---|
| Bill Gates | billg@microsoft.com |
| President Clinton | president@whitehouse.com |
| Stephen Walther | swalther@somewhere.com |
傳統程式習慣「迴圈掃描第 N 筆」,但 SQL 不支援位置存取:
WHERE name = 'Bill Gates'初學者常因這點受挫,但這正是 SQL 高效能的關鍵。
SELECT email
FROM email_table
WHERE name = "Bill Gates"
三大子句:
AND:同時滿足多條件(縮小結果)OR:滿足任一條件(擴大結果)NOT:排除特定條件-- Bill Gates 或 Clinton 的所有信箱
SELECT email FROM email_table
WHERE name = "Bill Gates" OR name = "president Clinton"
語法直覺類似自然語言:「從某表選取某欄,條件是...」。