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)
SQL语法介绍的更多相关文章
- FreeSql (二十四)Linq To Sql 语法使用介绍
原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法 ...
- [转]MySQL 最基本的SQL语法/语句
MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下. DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...
- 初学者SQL语句介绍
初学者SQL语句介绍 1.用 Select 子句检索记录 Select 子句是每一个检索数据的查询核心.它告诉数据库引擎返回什么字段. Select 子句的常见形式是: S ...
- [Android新手区] SQLite 操作详解--SQL语法
该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法 :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...
- SQLite的SQL语法
SQLite库能够解析大部分标准SQL语言.但它也省去了一些特性而且增加了一些自己的新特性.这篇文档就是试图描写叙述那些SQLite支持/不支持的SQL语法的.查看keyword列表. 例如以下语法表 ...
- SQLite的 SQL语法总结
SQLite库可以解析大部分标准SQL语言.但它也省去了一些特性并且加入了一些自己的新特性.这篇文档就是试图描述那些SQLite支持/不支持的SQL语法的.查看关键字列表. 如下语法表格中,纯文本用蓝 ...
- 面试必须要知道的SQL语法,语句
面试必须要知道的SQL语法,语句 收藏 asc 按升序排列desc 按降序排列下列语句部分是Mssql语句,不可以在access中使用.SQL分类: DDL—数据定义语言(Create,Alter,D ...
- pl/sql的介绍
为什么需要pl/sql编程? 因为使用纯的sql语句来操作数据库,有先天性的技术缺陷: 1.不能模块编程: 2.执行速度慢: 3.安全性有问题: 4.浪费带宽. pl/sql是什么? pl/sql(p ...
- SQL的介绍及MySQL的安装
基础篇 - SQL 介绍及 MySQL 安装 SQL的介绍及MySQL的安装 课程介绍 本课程为实验楼提供的 MySQL 实验教程,所有的步骤都在实验楼在线实验环境中完成, ...
随机推荐
- Linux 01 Liunx系统简介
Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位 ...
- 《流畅的Python》Object References, Mutability, and Recycling--第8章
Object References, Mutability, and Recycling 本章章节: Variables Are Not Boxes identity , Equality , Al ...
- python_反射:动态导入模块
官方推荐方法: test_mod.py def hi(): print('Hi') test.py import importlib q = importlib.import_module('test ...
- flutter 编译报错 ../extended_network_image_provider.dart:63:41: Error: Type 'DecoderCallback' not found
flutter 编译的时候报错 Compiler message:../../../.pub-cache/hosted/pub.dartlang.org/extended_image_library- ...
- SEERC 2018 B. Broken Watch (CDQ分治)
题目链接:http://codeforces.com/gym/101964/problem/B 题意:q 种操作,①在(x,y)处加一个点,②加一个矩阵{(x1,y1),(x2,y2)},问每次操作后 ...
- Shell 脚本来自动监控 Linux 系统的内存
# vim /scripts/swap-warning.sh #!/bin/bash #提取本服务器的IP地址信息 IP=`ifconfig eth0 | grep "inet addr&q ...
- 连续攻击游戏【P1640洛谷】二分图匹配变形【好题】【每次memset太慢了,用时间戳id。】
lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...
- Oracle 后台进程(四)CKPT进程
一.CKPT简介 检查点是一个数据库事件,它把修改数据从高速缓存写入磁盘,并更新控制文件和数据文件.检查点分为三类:1)局部检查点:单个实例执行数据库所有数据文件的一个检查点操作,属于此实例的全部脏缓 ...
- xgzc— math 专题训练(二)
费马小定理&欧拉定理 费马小定理: 如果\(p\)是一个质数,而整数\(a\)不是\(p\)的倍数,\(a^{p-1}\equiv1\pmod p\) 欧拉定理: 当\(a\)与\(n\)互质 ...
- C语言学习笔记8-函数
C语言学习笔记8-函数 ...待编辑 1.汇编看函数调用过程 2.函数调用过程图示:学好C这个是关键,要懂得原理 标准调用(_cdecl) 参数由右往左入栈,调用者平衡栈(即入多少参数后参数调用玩后 ...