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 实验教程,所有的步骤都在实验楼在线实验环境中完成, ...
随机推荐
- Bind Mounts and File System Mount Order
When you use the bind option of the mount command, you must be sure that the file systems are m ...
- 《浏览器工作原理与实践》<10>作用域链和闭包 :代码中出现相同的变量,JavaScript引擎是如何选择的?
在上一篇文章中我们讲到了什么是作用域,以及 ES6 是如何通过变量环境和词法环境来同时支持变量提升和块级作用域,在最后我们也提到了如何通过词法环境和变量环境来查找变量,这其中就涉及到作用域链的概念. ...
- 关于zsh在使用scp时报错zsh: no matches found: scp
root@banxia:scp root@172.16.13.150:/123/* . zsh: no matches found: root@172.16.13.150:/123/* root@ba ...
- go语言信号量的使用例子
//shared values are passed around on channels // 信号量. /* var sem = make(chan int, MaxOutstanding) fu ...
- linux实操_shell位置参数变量
基本语法: 脚本内容: 输出效果:
- 将 Python 程序打包成 .exe 文件
1.简介 做了一个excel的风控模板,里面含有宏,我用python的第三方xlwings部署到linux后发现,linux环境并不支持xlwings. Python 程序都是脚本的方式,一般是在解析 ...
- SessionState,默認mode應該是"InProc"
在ASP.NET的sessionState的三種屬性 http://www.dotblogs.com.tw/boei/archive/2010/07/06/16414.aspx需要在另外的config ...
- How to change the default browser to debug with in Visual Studio 2008?
http://stackoverflow.com/questions/297298/how-to-change-the-default-browser-to-debug-with-in-visual- ...
- Cogs 58. 延绵的山峰(st表)
延绵的山峰 ★★☆ 输入文件:climb.in 输出文件:climb.out 简单对比 时间限制:1 s 内存限制:512 MB 问题描述 有一座延绵不断.跌宕起伏的山,最低处海拔为0,最高处海拔不超 ...
- csps模拟73-74
模拟73: T1:哔-------------------- sb模拟,然而一个小细节打炸了,不想解释(吐嘈大样例没有右移)... #include<iostream> #include& ...