SQL语法介绍
一、Select 查询
语法:
mysql> help select
Name: 'SELECT'
Description:
Syntax:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
1、Limit
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
mysql> select * from employees order by emp_no limit 50,5; (从50条,向后偏移5个)
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 10051 | 1953-07-28 | Hidefumi | Caine | M | 1992-10-15 |
| 10052 | 1961-02-26 | Heping | Nitsch | M | 1988-05-21 |
| 10053 | 1954-09-13 | Sanjiv | Zschoche | F | 1986-02-04 |
| 10054 | 1957-04-04 | Mayumi | Schueller | M | 1995-03-13 |
| 10055 | 1956-06-06 | Georgy | Dredge | M | 1992-04-27 |
+--------+------------+------------+-----------+--------+------------+
5 rows in set (0.00 sec)
2、order by 排序
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...] 默认为升序asc
mysql> select * from dept_emp order by emp_no limit 5;
+--------+---------+------------+------------+
| emp_no | dept_no | from_date | to_date |
+--------+---------+------------+------------+
| 10001 | d005 | 1986-06-26 | 9999-01-01 |
| 10002 | d007 | 1996-08-03 | 9999-01-01 |
| 10003 | d004 | 1995-12-03 | 9999-01-01 |
| 10004 | d004 | 1986-12-01 | 9999-01-01 |
| 10005 | d003 | 1989-09-12 | 9999-01-01 |
+--------+---------+------------+------------+
5 rows in set (0.00 sec)
mysql> select * from dept_emp order by emp_no desc limit 5;
+--------+---------+------------+------------+
| emp_no | dept_no | from_date | to_date |
+--------+---------+------------+------------+
| 499999 | d004 | 1997-11-30 | 9999-01-01 |
| 499998 | d002 | 1993-12-27 | 9999-01-01 |
| 499997 | d005 | 1987-08-30 | 9999-01-01 |
| 499996 | d004 | 1996-05-13 | 9999-01-01 |
| 499995 | d004 | 1997-06-02 | 9999-01-01 |
+--------+---------+------------+------------+
5 rows in set (0.00 sec)
3、where 过滤
mysql> select * from titles where title = 'Senior Engineer';
表达式:
>、=、<
and
or
not
between NUM1 and NUM2
like 模式匹配
mysql> select * from titles where emp_no between 10001 and 10004;
+--------+-----------------+------------+------------+
| emp_no | title | from_date | to_date |
+--------+-----------------+------------+------------+
| 10001 | Senior Engineer | 1986-06-26 | 9999-01-01 |
| 10002 | Staff | 1996-08-03 | 9999-01-01 |
| 10003 | Senior Engineer | 1995-12-03 | 9999-01-01 |
| 10004 | Engineer | 1986-12-01 | 1995-12-01 |
| 10004 | Senior Engineer | 1995-12-01 | 9999-01-01 |
+--------+-----------------+------------+------------+
5 rows in set (0.00 sec)
连表查询
内连接
mysql> select e.emp_no,concat(last_name,' ',first_name) as name,gender,title from employees as e,titles as t where e.emp_no = t.emp_no order by emp_no limit 10;
外连接
A:左外连接
mysql> select * from a;
+------+
| a |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> select * from n;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql> select * from a left join n on a.a=n.a;
+------+------+
| a | a |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | NULL |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from a;
+-----------+--------+
| course_id | course |
+-----------+--------+
| 1 | yuwen |
| 2 | shuxue |
| 3 | lishi |
| 4 | yingyu |
| 5 | huaxue |
+-----------+--------+
5 rows in set (0.00 sec)
mysql> select * from b;
+-----+--------+-----------+
| sid | name | course_id |
+-----+--------+-----------+
| 1 | wang | 1 |
| 2 | limeng | 3 |
| 3 | liu | 1 |
| 4 | wangli | 4 |
| 5 | di | 2 |
| 6 | kaiu | 3 |
+-----+--------+-----------+
6 rows in set (0.00 sec)
mysql> select * from b left join a on b.course_id = a.course.id;
ERROR 1054 (42S22): Unknown column 'a.course.id' in 'on clause'
mysql> select * from b left join a on b.course_id = a.course_id;
+-----+--------+-----------+-----------+--------+
| sid | name | course_id | course_id | course |
+-----+--------+-----------+-----------+--------+
| 1 | wang | 1 | 1 | yuwen |
| 3 | liu | 1 | 1 | yuwen |
| 5 | di | 2 | 2 | shuxue |
| 2 | limeng | 3 | 3 | lishi |
| 6 | kaiu | 3 | 3 | lishi |
| 4 | wangli | 4 | 4 | yingyu |
+-----+--------+-----------+-----------+--------+
6 rows in set (0.00 sec)
mysql> select * from b right join a on b.course_id = a.course_id;
+------+--------+-----------+-----------+--------+
| sid | name | course_id | course_id | course |
+------+--------+-----------+-----------+--------+
| 1 | wang | 1 | 1 | yuwen |
| 3 | liu | 1 | 1 | yuwen |
| 5 | di | 2 | 2 | shuxue |
| 2 | limeng | 3 | 3 | lishi |
| 6 | kaiu | 3 | 3 | lishi |
| 4 | wangli | 4 | 4 | yingyu |
| NULL | NULL | NULL | 5 | huaxue |
+------+--------+-----------+-----------+--------+
7 rows in set (0.00 sec)
mysql> select c.customer_id,count(o.order_id) as total_orders from customers as c left join orders as o on c.customer_id = o.customer_id where city = 'HangZhou' group by c.customer_id having total_orders <2 order by total_orders desc;
+-------------+--------------+
| customer_id | total_orders |
+-------------+--------------+
| TX | 1 |
| baidu | 0 |
+-------------+--------------+
2 rows in set (0.00 sec)