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 实验教程,所有的步骤都在实验楼在线实验环境中完成, ...
随机推荐
- libssh
1.SSH概念 ssh(secure shell),安全外壳协议,由IETF的网络小组所制定.ssh为建立在应用层基础上的安全协议.SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议. ...
- MySQL的MHA实现高可用性
MySQL高可用 (1)MMM: Multi-Master Replication Manager for MySQL,Mysql主主复制管理器是一套灵活的脚本程序,基于perl实现,用来对mysql ...
- powerlink的Windows-DEMO生成笔记
资料准备: 1.Visual studio 2010 2.Cmake 3.Powerlink 2.7.1源码 具体下载请到相关页面去获取. 新版的powerlink分为两个部分: 1.协议栈 2.应用 ...
- Python基础之While循环
一.摘要 本片博文将介绍input()函数和while循环的使用 二.input()函数 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以 ...
- python画图matplolib
http://python.jobbole.com/85106/ 1.画二维图 2.画三维图 我的电脑只能在jupyter notebook上面运行才能看的到,常规import库 %matplotli ...
- 根据IP获取经纬度 js
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- WPF界面开发者注意啦!Scheduler控件支持时区功能了,你get了吗
DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...
- python utf8
1. # coding:utf-8 作用是定义源代码的编码. 如果没有定义, 此源码中是不可以包含中文字符串的.PEP 0263 -- Defining Python Source Code Enco ...
- python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch
协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...
- inline-block默认间距
inline-block元素默认会有间距 解决方法之移除空格 元素间留白间距出现的原因就是标签段之间的空格,因此,去掉HTML中的空格,自然间距就木有了.考虑到代码可读性,显然连成一行的写法是不可 ...