| select
語法是關連式資料庫最常用的部份,可以取得你想要的資料。這是一個簡單的範例:
select "column1"[,"column2",etc] from "tablename"
[where "condition"];
[] = optional
在 select 之後所列欄位名,則是要顯示的資料部份。你可以一一列出各欄位名,或是以
"*" (星號)來代表選擇全部的欄位。
"from"
之後放資料表名稱。
"where" 子句 (選項)
指定要過濾的條件,符合者才會顯現。
where 可使用的條件運算元:
=
等於
> 大於
< 小於
>= 大或等於
<= 小或等於
<> 大相等
LIKE *相似
For example:
select first, last, city
from empinfo
where first LIKE 'Er%';
以'Er' 為開頭的字串就符合。要以單引號框住。
Or you can specify,
select first, last
from empinfo
where last LIKE '%s';
最後一字為 S 就符合。
select * from empinfo
where first = 'Eric';
則表示 first name 一定要是
'Eric' 才可以。
範例資料表"empinfo"
| first |
last |
id |
age |
city |
state |
| John |
Jones |
99980 |
45 |
Payson |
Arizona |
| Mary |
Jones |
99982 |
25 |
Payson |
Arizona |
| Eric |
Edwards |
88232 |
32 |
San Diego |
California |
| Mary Ann |
Edwards |
88233 |
32 |
Phoenix |
Arizona |
| Ginger |
Howell |
98002 |
42 |
Cottonwood |
Arizona |
| Sebastian |
Smith |
92001 |
23 |
Gila Bend |
Arizona |
| Gus |
Gray |
22322 |
35 |
Bagdad |
Arizona |
| Mary Ann |
May |
32326 |
52 |
Tucson |
Arizona |
| Erica |
Williams |
32327 |
60 |
Show Low |
Arizona |
| Leroy |
Brown |
32380 |
22 |
Pinetop |
Arizona |
| Elroy |
Cleaver |
32382 |
22 |
Globe |
Arizona |
可以在以下文字方塊中輸入你了查詢字串,來查看結果。
select first, last, city from empinfo;
select last, city, age from empinfo
where age > 30;
select first, last, city, state from empinfo
where first LIKE 'J%';
select * from empinfo;
select first, last from empinfo
where last LIKE '%s';
select first, last, age from empinfo
where last LIKE '%illia%';
select * from empinfo where first = 'Eric';
語法練習
Enter select statements
to:
1 顯示這張表中所有人的名(first name)、年級(age)
2 顯示這張表中所有人的名(first name)、姓(last name)、城市(city),條件為非來自Payson
的人。
3 顯示超過40歲人的所有欄位資料。
4 顯示這張表中所有人的名(first name)、姓(last name),條件為姓( last
name )是以"ay" 為結尾。
5 顯示名字為 "Mary" 的人所有欄位資料。
6 顯示名字包含有 "Mary" 字元的人所有欄位資料。
Answers to these exercises
SQL Interpreter
|