天天看點

mysql linux selected_MySQL的查詢語句--SELECT

本來想總結高可用叢集的另外幾個實驗呢,回頭看看别人總結的内容,好精細,而且擴充了好多内容,慚愧的不行,還是先跳過了,呵呵~~~

介紹了簡單的資料庫操作等,接下來從細節入手,來介紹mysql的查詢語句;

在這裡導入了一個jiaowu資料庫,來實作以下例題的操作:

先來看下這個資料庫所包含的内容

mysql>show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| jiaowu |

| mysql |

| test |

+--------------------+

4 rows in set (0.00 sec)

l> use jiaowu;

Database changed

mysql>show tables;

+------------------+

| Tables_in_jiaowu |

+------------------+

| courses |

| scores |

| students |

| tutors |

+------------------+

4 rows in set (0.00 sec)mysql>select * from students;

+-----+--------------+------+--------+------+------+------+---------------------+

| SID | Name | Age | Gender | CID1 | CID2 | TID | CreateTime |

+-----+--------------+------+--------+------+------+------+---------------------+

| 1 | GuoJing | 19 | M | 2 | 7 | 3 | 2012-04-06 10:00:00 |

| 2 | YangGuo | 17 | M | 2 | 3 | 1 | 2012-04-06 10:00:00 |

| 3 | DingDian | 25 | M | 6 | 1 | 7 | 2012-04-06 10:00:00 |

| 4 | HuFei | 31 | M | 8 | 10 | 5 | 2012-04-06 10:00:00 |

| 5 | HuangRong | 16 | F | 5 | 9 | 9 | 2012-04-06 10:00:00 |

| 6 | YueLingshang | 18 | F | 8 | 4 | NULL | 2012-04-06 10:00:00 |

| 7 | ZhangWuji | 20 | M | 1 | 7 | NULL | 2012-04-06 10:00:00 |

| 8 | Xuzhu | 26 | M | 2 | 4 | NULL | 2012-04-06 10:00:00 |

| 9 | LingHuchong | 22 | M | 11 | NULL | NULL | 2012-04-06 10:00:00 |

| 10 | YiLin | 19 | F | 18 | NULL | NULL | 2012-04-06 10:00:00 |

+-----+--------------+------+--------+------+------+------+---------------------+

10 rows in set (0.00 sec)mysql>select * from tutors;

+-----+--------------+--------+------+

| TID | Tname | Gender | Age |

+-----+--------------+--------+------+

| 1 2 | HuangYaoshi | M | 63 |

| 3 | Miejueshitai | F | 72 |

| 4 | OuYangfeng | M | 76 |

| 5 | YiDeng | M | 90 |

| 6 | YuCanghai | M | 56 |

| 7 | Jinlunfawang | M | 67 |

| 8 | HuYidao | M | 42 |

| 9 | NingZhongze | F | 49 |

+-----+--------------+--------+------+

9 rows in set (0.00 sec)

這是以下例題中會用到的資料,可以先參考下;

首先是mysql查詢語句:

查詢的分類:

單表查詢:簡單查詢

多表查詢:聯結查詢

子查詢:複雜查詢

聯合查詢

select語句:

常用函數:

##field--表示字段

count(*) 總行數

mysql>select count(*) from tutors;

+----------+

| count(*) |

+----------+

| 9 |

+---------+

1 row in set (0.00 sec)

max(field) 傳回最大值

mysql>select max(age) from tutors;

+----------+

| max(age) |

+----------+

| 93 |

+----------+

1 row in set (0.00 sec)

min(field) 傳回最小值

avg(field) 平均值

mysql>select avg(age) from tutors;

+----------+

| avg(age) |

+----------+

| 67.5556 |

+----------+

1 row in set (0.00 sec)

sum() 記和

mysql>select sum(1+2);

+----------+

| sum(1+2) |

+----------+

| 3 |

+----------+

1 row in set (0.01 sec)

select 是挑選列的,where是挑選行的,二者結合起來才是将一個實體的屬性整體顯示出來

where 後面指定的是條件:

可以指定的條件有:

算術比較:

> , < , = , !,>= , <=, <=> (取得的結果是空值也不會出錯)

mysql>select name,age from students where age>=20;

+-------------+------+

| name | age |

+-------------+------+

| DingDian | 25 |

| HuFei | 31 |

| ZhangWuji | 20 |

| Xuzhu | 26 |

| LingHuchong | 22 |

+-------------+------+

5 rows in set (0.00 sec)

組合邏輯比較:

and

or

not(!)

mysql>select name,age from students where ! (age<=25);

+-------+------+

| name | age |

+-------+------+

| HuFei | 31 |

| Xuzhu | 26 |

+-------+------+

2 rows in set (0.00 sec)

其他條件比較:

beween …… and ……

mysql>select name,age from students where age between 24 and 30

+----------+------+

| name | age |

+----------+------+

| DingDian | 25 |

| Xuzhu | 26 |

+----------+------+

2 rows in set (0.00 sec)

in 查詢的字段在指定的清單中

mysql>select name,age from students where age in (18,20,25);

+--------------+------+

| name | age |

+--------------+------+

| DingDian | 25 |

| YueLingshang | 18 |

| ZhangWuji | 20 |

+--------------+------+

3 rows in set (0.01 sec)

is null:查詢是空值的

mysql>select name from students where cid2 is null;

+-------------+

| name |

+-------------+

| LingHuchong |

| YiLin |

+-------------+

2 rows in set (0.00 sec)

is not null

like : 做通配符的比對

%:比對任意長度的任意字元

_: 比對單個字元

regexp|rlike : 正規表達式的比對

order by: 排序,預設是升序的asc

desc:降序

mysql>select name,age from students where age in (22,18,25)order by age desc;

+--------------+------+

| name | age |

+--------------+------+

| DingDian | 25 |

| LingHuchong | 22 |

| YueLingshang | 18 |

+--------------+------+

3 rows in set (0.00 sec)

distinct: 顯示結果的唯一性,附在select之後(以下面的例子解說,cid1相同的隻顯示了一次)

mysql>select distinct cid1 from students order by cid1 desc;

+------+

| cid1 |

+------+

| 18 |

| 11 |

| 8 |

| 6 |

| 5 |

| 2 |

| 1 |

+------+

7 rows in set (0.00 sec)

group by: 将取得的結果進行分組,通常分組的結果是用來做聚合運算的

having: 對分組的結果進行條件過濾

mysql>select avg(age),cid1 from students group by cid1;

+----------+------+

| avg(age) | cid1 |

+----------+------+

| 20.0000 | 1 |

| 20.6667 | 2 |

| 16.0000 | 5 |

| 25.0000 | 6 |

| 24.5000 | 8 |

| 22.0000 | 11 |

| 19.0000 | 18 |

+----------+------+

7 rows in set (0.00 sec)

limit:限定顯示的行數

eg:limit 1,2;表示跳過第一行再顯示兩行

mysql>select avg(age),cid1 from students group by cid1 limit 1,2;

+----------+------+

| avg(age) | cid1 |

+----------+------+

| 20.6667 | 2 |

| 16.0000 | 5 |

+----------+------+

2 rows in set (0.00 sec)

mysql linux selected_MySQL的查詢語句--SELECT