MySQL学习笔记(Day011:SELECT)
@(MySQL学习)
[TOC]
一. SELECT语法介绍
1 | SELECT |
二. LIMIT 和 ORDER BY
1 | mysql> select * from employees limit 1; -- 从employees中 随机 取出一条数据,结果是不确定的 |
ORDER BY
是把已经查询好的结果集进行排序
三. WHERE
WHERE
是将查询出来的结果,通过WHERE
后面的条件(condition),对结果进行过滤1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50mysql> select * from employees where emp_no > 30000 emp_no limit 4; -- 不加order by的limit是不确定的SQL
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 30001 | 1953-03-27 | Izaskun | Morton | M | 1988-05-21 |
| 30002 | 1960-08-23 | Branimir | Snedden | M | 1998-09-24 |
| 30003 | 1952-11-25 | Takahito | Vilarrasa | M | 1990-08-22 |
| 30004 | 1957-11-26 | Lucian | Penttonen | F | 1992-10-08 |
+--------+------------+------------+-----------+--------+------------+
4 rows in set (0.00 sec)
mysql> select * from employees where emp_no > 40000 order by emp_no limit 4;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 40001 | 1956-03-28 | Akemi | Maliniak | F | 1987-08-06 |
| 40002 | 1960-03-15 | Nakhoon | Badr | M | 1990-02-13 |
| 40003 | 1960-01-26 | Jacopo | Marshall | F | 1991-09-30 |
| 40004 | 1955-09-09 | Anneke | Stiles | F | 1986-03-05 |
+--------+------------+------------+-----------+--------+------------+
4 rows in set (0.02 sec)
mysql> select * from employees
-> where emp_no > 40000
-> and hire_date > '1991-01-01' -- 可以用 and 进行 逻辑与
-> order by emp_no limit 4;
+--------+------------+------------+------------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+------------+--------+------------+
| 40003 | 1960-01-26 | Jacopo | Marshall | F | 1991-09-30 |
| 40005 | 1961-02-27 | Zsolt | Fairtlough | F | 1991-07-08 |
| 40012 | 1955-02-07 | Chinhyun | Ozeri | F | 1995-08-12 |
| 40015 | 1964-10-08 | Ioana | Lemarechal | M | 1997-08-07 |
+--------+------------+------------+------------+--------+------------+
4 rows in set (0.00 sec)
mysql> select * from employees
-> where (emp_no > 40000 and birth_date > '1961-01-01') -- 使用()明确条件的逻辑规则
-> or (emp_no > 40000 and hire_date > '1991-01-01') -- 可以使用 or 做 逻辑或
-> order by emp_no limit 5;
+--------+------------+------------+------------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+------------+--------+------------+
| 40003 | 1960-01-26 | Jacopo | Marshall | F | 1991-09-30 |
| 40005 | 1961-02-27 | Zsolt | Fairtlough | F | 1991-07-08 |
| 40006 | 1962-11-07 | Basim | Panienski | F | 1986-12-27 |
| 40012 | 1955-02-07 | Chinhyun | Ozeri | F | 1995-08-12 |
| 40015 | 1964-10-08 | Ioana | Lemarechal | M | 1997-08-07 |
+--------+------------+------------+------------+--------+------------+
5 rows in set (0.00 sec)
四. JOIN
4.1. INNER JOIN
1 | -- |
4.2. OUTER JOIN
1 | -- |
4.3. GROUP BY
1 | -- |