mysql 查詢子句

樣板

create database sky;

use sky;

create table m1(

id int(11),

name char(20),

age tinyint(10),

sex enum('男','女'),

score tinyint(10),

address char(20)

) default charset=utf8;

insert into m1 values

(1,'L1',21,'男',90,'北京'),

(2,'L2',19,'男',91,'上海'),

(3,'L3',24,'女',95,'廣州'),

(4,'L4',22,'男',89,'廣州'),

(5,'L5',20,'女',86,'上海'),

(6,'L6',19,'女',99,'廣州');



執行順序

1. select * from *

2. where 條件語句 #只篩選表裏存在的字段

3. group by 分組

4. having 篩選

5. order by 排序高低

6. limit 顯示幾行,降序或者升序


group by

作用:給查詢的結果進行分組,去重

1. group by 之後的字段必須爲select之後的字段

2. 如果select 之後的字段和group by之後的字段不一致,則必須對select之後的字段做聚合處理。

篩選有幾個城市

mysql> select address from m1 group by address;

+---------+

| address |

+---------+

| 上海    |

| 北京    |

| 廣州    |

+---------+

3 rows in set (0.00 sec)


having

作用:對查詢的結果進行進一步的篩選,只篩選通過計算出來的字段

1. having語句通常和group by語句聯合使用,用來過濾group by語句返回的記錄集

2. having語句的存在彌補了where關鍵字不能與聚合函數聯合使用的不足

取出分數大於等於90前兩名的城市


mysql> select address,avg(score) from m1

-> group by address

-> having avg(score) >= 90

-> order by avg(score) desc

-> limit 2;

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

| address | avg(score) |

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

| 廣州    |    94.3333 |

| 北京    |    90.0000 |

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

2 rows in set (0.00 sec)


order by

作用:對查詢的結果進行排序

語法:  order by 字段名  排序方法

排序方式:

ASC(默認) : 升序

DESC:降序

從高到低進行排序

mysql> select * from m1 order by score desc;

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

| id   | name | age  | sex  | score | address |

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

|    4 | L4   |   22 | 男   |   101 | 廣州    |

|    6 | L6   |   19 | 女   |    99 | 北京    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    2 | L2   | NULL | 男   |    91 | 上海    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

|    5 | L5   | NULL | 女   |    86 | 上海    |

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

6 rows in set (0.00 sec)


顯示address在北京和上海,age爲20+,的人按score 升序

mysql> select * from m1

-> where

-> address in("北京","廣州") and age like '2_'

-> order by score asc;

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

| id   | name | age  | sex  | score | address |

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

|    4 | L4   |   22 | 男   |    89 | 廣州    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

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

3 rows in set (0.00 sec)


limit

作用:限制顯示查詢記錄的條數

語法:

limit n 顯示n條記錄

limit m,n 從第m+1條記錄開始顯示,顯示n條

score字段降序排列,顯示前三名

mysql> select * from m1 order by score desc;

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

| id   | name | age  | sex  | score | address |

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

|    6 | L6   |   19 | 女   |    99 | 廣州    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

|    5 | L5   |   20 | 女   |    86 | 上海    |

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

6 rows in set (0.00 sec)


mysql> select * from m1 order by score desc limit 3;

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

| id   | name | age  | sex  | score | address |

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

|    6 | L6   |   19 | 女   |    99 | 廣州    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    2 | L2   |   19 | 男   |    91 | 上海    |

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

3 rows in set (0.00 sec)


socre字段降序從第二名開始顯示四條

mysql> select * from m1 order by score desc limit 1,4;

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

| id   | name | age  | sex  | score | address |

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

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    2 | L2   |   19 | 男   |    91 | 上海    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

|    4 | L4   |   22 | 男   |    89 | 廣州    |

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

4 rows in set (0.00 sec)


查詢age不爲空,成績前三的人

mysql> select * from m1;

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

| id   | name | age  | sex  | score | address |

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

|    2 | L2   | NULL | 男   |    91 | 上海    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

|    4 | L4   |   22 | 男   |   101 | 廣州    |

|    5 | L5   | NULL | 女   |    86 | 上海    |

|    6 | L6   |   19 | 女   |    99 | 北京    |

|    1 | L1   |   21 | 男   |    90 | 北京    |

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

6 rows in set (0.00 sec)


mysql> select * from m1

-> where age is not null

-> order by score desc

-> limit 3;

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

| id   | name | age  | sex  | score | address |

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

|    4 | L4   |   22 | 男   |   101 | 廣州    |

|    6 | L6   |   19 | 女   |    99 | 北京    |

|    3 | L3   |   24 | 女   |    95 | 廣州    |

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

3 rows in set (0.00 sec)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章