1、检索数据
1.1、检索单个列:SELECT prod_name FROM products;
1.2、检索多个列:SELECT prod_id, prod_name, prod_price FROM products;
1.3、检索所有列:SELECT * FROM products;
1.4、检索不同的行:SELECT DISTINCT vend_id FROM products;
1.5、限制结果:SELECT prod_name FROM products LIMIT 5;
                      SELECT prod_name FROM products LIMIT 3,4;   //表示返回从行3开始的4行
        注意,检索出来的第一行是行0,而不是行1。

2、检索排序数据
2.1、排序数据:SELECT prod_name FROM products ORDER BY prod_name;
2.2、按多个列排序:SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price, prod_name;
2.3、指定排序方向:SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC;
         多个列排序:SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC, prod_name ASC;
2.4、找出最高或最低的值:SELECT prod_price FROM products ORDER BY prod_price DESC limit 1;

3、过滤数据
3.1、使用where子句:SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50;
3.2、where子句操作符:=, <>, !=, <, <=, >, >=, BETWEEN
  3.2.1、检查单个值:SELECT prod_name, prod_price FROM products WHERE prod_name=‘fuses’;
  3.2.2、不匹配检查:SELECT vend_id, prod_name FROM products WHERE vend_id <> 1003;
  3.2.3、范围值检查:SELECT prod_name, prod_price FROM products WHERE prod_price BETWEEN 5 AND 10;
  3.2.4、空值检查:SELECT prod_name FROM products WHERE prod_price IS NULL;

4、数据过滤
4.1、组合where子句
  4.1.1、AND操作符:SELECT prod_id, prod_price, prod_name FROM products where vend_id = 1003 AND prod_price <= 10;
  4.1.2、OR操作符:SELECT prod_name, prod_price FROM products WHERE vend_id=1002 OR vend_id=1003;
  4.1.3、计算次序:SELECT prod_name, prod_price FROM products WHERE (vend_id = 1002 OR vend_id=1003) AND prod_price >= 10;
4.2、IN操作符:SELECT prod_name, prod_price FROM products WHERE vend_id IN  (1002, 1003) ORDER BY prod_name;
4.3、NOT操作符:SELECT prod_name, prod_price FROM products WHERE vend_id NOT IN (1002, 1003) ORDER BY prod_name;

5、用通配符进行过滤
5.1、LIKE操作符
  5.1.1、百分号(%)通配符:表示任何字符出现任何次数, SELECT prod_id, prod_name FROM products WHERE prod_name LIKE ‘jet%’;
  5.1.2、下划线(_)通配符:匹配单个字符,SELECT prod_id, prod_name FROM products WHERE prod_name LIKE ‘_ ton anvil’;

6、用正则表达式进行搜索
6.1、使用MySQL正则表达式
  6.1.1、基本字符匹配:SELECT prod_name FROM products WHERE prod_name REGEXP ‘1000’ ORDER BY prod_name;
                                SELECT prod_name FROM products WHERE prod_name REGEXP ‘.000’ ORDER BY prod_name;(LIKE匹配整个列,REGEXP匹配列中值)
  6.1.2、进行OR匹配:SELECT prod_name FROM products WHERE prod_name REGEXP ‘1000|2000’ ORDER BY prod_name;
  6.1.3、匹配几个字符之一:SELECT prod_name FROM products WHERE prod_name REGEXP ‘[123] Ton’ ORDER BY prod_name;
  6.1.4、匹配范围:SELECT prod_name FROM products WHERE prod_name REGEXP ‘[1-5] Ton’ ORDER BY prod_name;
  6.1.5、匹配特俗字符:SELECT prod_name FROM products WHERE prod_name REGEXP ‘\\.’ ORDER BY prod_name;

7、创建计算字段
7.1、拼接字段:SELECT Concat(vend_name, ‘(’, vend_country, ‘)’) FROM vendors ORDER BY vend_name;
7.2、使用别名:SELECT Concat(RTrim(vend_name), ‘(‘, RTrim(vend_country), ‘)’) AS vend_title FROM vendors ORDER BY vend_name;
7.3、执行算数计算:SELECT prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM order items WHERE order_num=20005;

8、使用数据处理函数
8.1、文本处理函数:SELECT vend_name, Upper(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name;
       常用文本处理函数:Left(), Length(), Locate(), Lower(), LTrim(), Right(), RTrim(), Soundex(), SubString(), Upper
8.2、日期和时间处理函数:SELECT cust_id, order_num FROM orders WHERE YEAR(order_date) = 2005 AND Month(order_date) = 9;
8.3、数值处理函数:abs(), cos(), exp()  等等。

9、汇总数据
9.1、AVG()函数:SELECT AVG(prod_price) AS avg_price FROM products;
9.2、COUNT()函数:SELECT COUNT(*) AS num_cust FROM customers;
9.3、MAX()函数:SELECT MAX(prod_price) AS max_price FROM products;
9.4、MIN()函数:SELECT MIN(prod_price) AS min_price FROM products;
9.5、SUM()函数:SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 2005;
9.6、聚集不同值:SELECT MIN(DISTINCT prod_price) AS min_price FROM products;

10、分组数据
10.2、创建分组:SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id;
10.3、过滤分组:SELECT cust_id, COUNT(*) AS orders FROM orders GROUP BY cust_id Having COUNT(*) >= 2;
                             SELECT vend_id, COUNT(*) AS num_prods FROM products WHERE prod_price >= 10 GROUP BY vend_id;
10.4、分组和排序:SELECT order_num, SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price) >= 50 ORDER BY ordertotal ;
10.5、SELECT子句顺序:
     SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT

11、使用子查询
SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM order items WHERE prod_id=’TNT2’);

SELECT cust_name, cust_state, (SELECT COUNT(*) FROM orders WHERE orders.cust_id = customers.cust_id) AS orders FROM customers ORDER BY dust_name;

12、联结表
SELECT vend_name, prod_name, prod_price FROM vendors, products WHERE vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;

SELECT vend_name, prod_name, prod_price FROM vendors INNER JOIN products ON vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;

SELECT vend_name, prod_name, prod_price FROM vendors, products, orderitems WHERE vendors.vend_id = products.vend_id AND orderitems.prod_id=produts.prod_id AND order_num=20005;

13、创建高级联结
13.1、自联结:SELECT p1.prod_id, p1.prod_name FROM products AS p1, products AS p2 WHERE p1.vend_id=p2.vend_id AND p2.prod_id=‘DTNTR';
13.2、外联结:SELECT customers.cust_id, orders.order_num FROM orders LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id;
13.3、使用带聚集函数的联结:SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_num) AS num_ord FROM customers INNER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;

14、插入数据
INSERT INTO customers(cuts_name, cuts_contact) VALUES (‘ppp’, ‘ppp');
INSERT INTO customers(cuts_name, cuts_contact) VALUES (‘ppp’, ‘ppp')('ttt', 'ttt')('qqq', 'qqq');


15、更新和删除数据

UPDATE customers SET cuts_name=’ttt’, cuts_email=‘eee@gmial.com’ WHERE cuts_id = 10005;
DELETE FROM customers WHERE cuts_id=10006;

16、变量的使用

变量的初始化和设置:set @age := 20;   set @age := @age + 1;

变量的查询:select @age;

17、if、case和coalesce函数

if函数:select if(1>2, 1, 2);   ->  输出2

case函数:select (case when 1>2 then 2 when 2>3 then 3 else 4 end);  ->  输出4

coalesce函数:select coalesce(null, 1);  ->  输出1

MYSQL学习笔记——常用语句的更多相关文章

  1. mysql学习一 常用语句

    操作系统为windows  1 启动关闭mysql服务 //windows mysqld --console //开启mysql服务 mysqladmin -uroot shutdown //关闭my ...

  2. MYSQL学习笔记——sql语句优化之索引

    上一篇博客讲了可以使用慢查询日志定位耗时sql,使用explain命令查看mysql的执行计划,以及使用profiling工具查看语句执行真正耗时的地方,当定位了耗时之后怎样优化呢?这篇博客会介绍my ...

  3. mysql学习笔记—常用sql语句

    sql注意事项: SQL 对大小写不敏感:SELECT 与 select 是相同的 某些数据库系统要求在每条 SQL 语句的末端使用分号. CREATE DATABASE CREATE DATABAS ...

  4. MYSQL学习笔记——sql语句优化工具

    优化sql:思路: 使用explan->先查询type类型看看是all还是ref,然后判断 possible_keys (显示可能应用在这张表中的索引, 一个或多个.查询涉及到的字段是若存在索引 ...

  5. mysql学习笔记—常用sql函数

    SQL 拥有很多可用于计数和计算的内建函数. SQL Aggregate 函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. 有用的 Aggregate 函数: AVG() ...

  6. MySql学习笔记(一)之DQL常用查询

    MySql学习笔记(一)之DQL常用查询 前言:mysql是中小型的数据库软件,SQL语言分为DDL,DCL,DML,DQL四种,在这里重点讲解DQL的单表查询. 正文:在学习mysql单表查询之前, ...

  7. mysql basic operation,mysql总结,对mysql经常使用语句的详细总结,MySQL学习笔记

    mysql> select * from wifi_data where dev_id like "0023-AABBCCCCBBAA" ; 1.显示数据库列表.show d ...

  8. Mysql学习笔记(三)对表数据的增删改查。

    正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...

  9. MySQL学习笔记一

    MySQL 学习笔记 一 一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系 ...

随机推荐

  1. [BZOJ1547]周末晚会:Burnside引理+DP

    分析 Attention!这道题的模数是\(1e8+7\). 注意到循环同构会被认为是同一种方案,我们可以把顺时针旋转每个人的位置作为置换,容易发现这些置换一定会形成一个置换群,于是题目所求的所有合法 ...

  2. [django]上下文管理器

    上下文管理器django提取context中的数据去供模板调用 需求: 所有的页面都需要一个特定的变量 本质: python函数 , 接收一个HttpRequest对象的参数 , 且返回的必须是一个字 ...

  3. fedora18 You might need to install dependency packages for libxcb.

    22 down vote The page Qt for X11 Requirements lists some packages required to build Qt on Debian. Th ...

  4. 从 Quora 的 187 个问题中学习机器学习和NLP

    从 Quora 的 187 个问题中学习机器学习和NLP 原创 2017年12月18日 20:41:19 作者:chen_h 微信号 & QQ:862251340 微信公众号:coderpai ...

  5. 常见的七种Hadoop和Spark项目案例

    常见的七种Hadoop和Spark项目案例 有一句古老的格言是这样说的,如果你向某人提供你的全部支持和金融支持去做一些不同的和创新的事情,他们最终却会做别人正在做的事情.如比较火爆的Hadoop.Sp ...

  6. mysqladmin processlist; show processlist/status/open tables/engine/variables/table status/columns/grants/index/privileges/innodb status/logs/warnings/////; 结果筛选

    mysqladmin showprocesslist可查看完整sql需要权限. SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此 ...

  7. customizable route planning 工业界地图产品的路径规划

    https://www.microsoft.com/en-us/research/publication/customizable-route-planning/?from=http%3A%2F%2F ...

  8. COUNT(*) vs COUNT(col)

    w https://www.percona.com/blog/2007/04/10/count-vs-countcol/

  9. 几种 MyBatis 增强插件

    1. mybatis-generator/mybatis-generator-gui 2. 通用mapper 3. mybatis-plus 4. fastmybatis 5. mybatis-enh ...

  10. MongoDB学习【四】—pymongo操作mongodb数据库

    一.pymongodb的安装 Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 PyMongo 驱动来连接. pip安装 pip 是一个通用的 Python 包管理工具, ...