• MySQL数据查询和函数


    查看数据库表信息

    show databases;//数据库列表
    use databasename;//使用此数据库
    show tables;//列表
    show columns from tablename;//列出表属性的信息
    describe tablenames;与上面等同
    

    查询语句

    select columnname from tablename;//检索单个列
    select column1,column2,column3 from tablename;//检索多个列
    select * from tablename;//检索所有列
    

    DISTINCT 检索出不同值的列

    LIMIT 限制返回的行数

    排序检索

    select column1 from tablename order by column1;//升序
    select column1,column2 from tablename order by column1, column2;//多个列排序
    select column1 from tablename order by column1 DESC;//降序
    select column1 from tablename order by column1 ASC;//升序
    

    使用ORDER BY 和LIMIT 可以找出列中最高或最低的值
    SELECT COLUMN1 FROM TABLE1 ORDER BY COLUMN1 LIMIT 1;

    过滤数据

    WHERE搜索条件

    select column1 from table1 where column1 = x;
    select column1 from table1 where column1 between x1 and x2;//范围值检索
    select column1 from table1 where column1 is null;//空值检索
    

    组合WHERE子句,AND,OR

    select column1, column2 from table1 where column1 = 1 and column2 = 2;//两个过滤条件
    

    IN指定条件范围

    select column1, column2 from table1 where column1 in (x1,x2) order by asc/desc;//在x1,x2范围内检索
    

    NOT否定,配合IN

    select column1 from table1 where column1 not in (x1,x2) order by column1;
    

    用通配符进行过滤

    LIKE谓词

    %通配符,%为任意字符

    select column1 from table1 where colomn1 LIKE 'jet%';//查找jet开头的词
    

    _通配符,匹配单个字符

    select column1 from table1 where column1 like '_ anvi1'//anvi1前只能匹配一个字符
    

    正则表达式搜索

    过滤条件复杂,正则表达式更有效解决
    关键字REGEXP

    select column1 from table1 where column1 REGEXP '.000';//
    
    • [123]表示1or2or3,[]是一种OR语句,[0-9]表示0到9
    • 匹配特殊字符.,.即可
    • *0个或者多个匹配
    • +1个或者多个匹配
    • ?0个或者1个匹配
    • {n}n个匹配

    计算字段

    从数据库中检索出转换、计算或者格式化过的数据,而不是在客户端应用程序重新格式化

    拼接字段Concat(),把多个串连接起来

    select Concat(column1 , '(', column2,")") from table1;//输出效果为column1(column2)
    

    用AS关键字赋予别名,别名也称为导出列

    使用数据处理函数

    • RTrim()去掉列值右边的空格
    • Upper()转换为大写
    • Left()返回串左边的字符
    • Length()返回串的长度
    • Lower()转换为小写
    • SubString()返回子串的字符
    日期和时间的处理函数
    • AddDate()增加一个日期,天,周
    • AddTime()增加一个时间,时,分
    • CurDate()返回当前日期
    • CurTime()返回当前时间
    • DateDiff()返回两个日期之差
      日期格式为yyyy-mm-dd
    SELECT datetime FROM table1 WHERE Date(datetime) = '2015-09-01';//过滤条件:日期20150901
    
    数值处理函数
    • Abs()绝对值
    • Exp()指数
    • Rand()随机数
    • Sqrt()平方根

    聚集函数

    汇总表的数据,确定表中行数,获得表中行组的和,找出最大值最小值

    • AVG()平均值
    • COUNT()返回某列行数
    • MAX()某列的最大值
    • MIN()某列的最小值
    • SUM()某列之和
    SELECT COUNT(*) AS num_items,
           MIN(prod_price) AS price_min,
           MAX(prod_price) AS price_max,
           AVG(prod_price) AS price_avg
    FROM products;
    

    分组数据

    GROUP BY以什么分组

    • 使用WITH ROLLUP 可以得到每个分组一级每个分组汇总级别的值
    • WHERE过滤行,HAVING过滤分组
    • HAVING支持所有WHERE操作符
    HAVING在数据分组后过滤,WHERE在数据分组前过滤。

    SELECT子句顺序

    1. SELECT返回的列和表达式
    2. FROM从中检索数据的表
    3. WHERE行级过滤
    4. GROUP BY分组过滤
    5. HAVING 组级过滤
    6. ORDER BY 排序
    7. LIMIT 检索行数

    子查询

    嵌套在其他查询中的查询

    SELECT cust_id FROM orders WHERE order_num IN( SELECT order_num FROM orderitems WHERE prod_id = 'TNT2');
    
  • 相关阅读:
    strcpy的实现
    使用Highcharts生成柱状图
    使用Highcharts结合PHP与Mysql生成饼状图
    shopnc 学习笔记
    php中关于mysqli和mysql区别的一些知识点分析
    jquery捕获enter键 按enter键执行提交
    php在IE浏览器中保存SESSION
    ShopNC 商城系统开发经验分享第五篇: 缓存设计
    ShopNC 商城系统开发经验分享第二篇:ShopNC商城系统初步分析
    SHOPEX于SHOP++比较哪款更适合做二次开发,为什么?
  • 原文地址:https://www.cnblogs.com/chenshaowei/p/12395253.html
Copyright © 2020-2023  润新知