一、select查询

//查询某张表所有数据
select * from temp; //查询指定列和条件的数据
//查询name和age这两列,age等于22的数据
select name,age from temp where age = 22; //as对列重命名
//as可以省略不写,如果重命名的列名出现特殊字符,如单引号,那就需要用双引号引在外面
select name as '名称' from temp; //给table去别名
select t.name Name from temp as t; //where条件查询
>、>=、<、<=、=、<>都可以出现在where语句中
select from t where a > 2 or a>=3 or a<5 or a<=6 or a=7 or a<>0; //and 并且
//查询名称等于Jack并且年龄大于20的
select * from temp where age > 20 and name = 'jack'; //or或者
--满足一个条件即可
select * from temp where name = 'jack' or name = 'jackson'; //between v and v2
--大于等于v且小于等于v2
select * from temp where age between 20 and 25; //in 查询
--可以多个条件,类似于or
--查询id 在括号中出现的数据
select *from temp where id in (1, 2, 3); //like模糊查询
--查询name以j开头的
select * from temp where name like 'j%';
--查询name包含k的
select * from temp where name like '%k%';
--escape转义,指定\为转义字符,上面的就可以查询name中包含“_”的数据
select * from temp where name like '\_%' escape '\'; //is null、is not null
--查询为null的数据
select * from temp where name is null;
--查询不为null 的数据
select * from temp where name is not null; //order by
--排序,升序(desc)、降序(asc)
--默认升序
select * from temp order by id;
select * from temp order by id asc;
--多列组合
select * from temp order by id, age; //not
select * from temp where not (age > 20);
select * from temp where id not in(1, 2); //distinct去掉重复数据
select distinct id from temp;
//多列将是组合的重复数据
select distinct id, age from temp; //查询常量
select 5+2;
select concat('a', 'bbb'); //concat函数,字符串连接
//concat和null进行连接,会导致连接后的数据成为null
select concat(name, '-eco') from temp; //对查询的数据进行运算操作
select age +2, age / 2, age - 2, age * 2 from temp where age - 2 > 22;

 二、函数

函数类似于存储过程,只是调用方式不同

//创建函数
create function addAge(age int) returns int
return age + 5;
//使用函数:
select addAge(age) from temp;
//删除函数
drop function if exists addAge;
drop function addAge;
//显示创建语法
show create function addAge;

三、触发器

触发器分为insert、update、delete三种触发器事件类型,还有after、before触发时间

//创建触发器
create trigger trg_temp_ins
before insert
on temp for each row
begin
insert into temp_log values(NEW.id, NEW.name);
end
//删除触发器
drop trigger trg_temp_ins

MySQL之select查询、function函数的更多相关文章

  1. mysql DML select查询

    windows上的操作 1.从官网下载mysql 下载navicat,用来连接mysql的 2.打开运行启动mysql 3.在navicat上的连接打开新建连接 然后输入链接名,连接名就是用户名,自己 ...

  2. MySQL使用select查询时,在查询结果中增加一个字段并指定固定值

    假设需求是这样的: mysql> desc user; +-------+----------+------+-----+---------+----------------+ | Field ...

  3. mysql的select查询语句

    1.简单查询 mysql> select * from students; +------------+----------+------+------+ | id | sname | sex ...

  4. 转载《mysql 一》:mysql的select查询语句内在逻辑执行顺序

    原文:http://www.jellythink.com/archives/924 我的抱怨 我一个搞应用开发的,非要会数据库,这不是专门的数据库开发人员干的事么?话说,小公司也没有数 据库开发人员这 ...

  5. mysql之select查询:练习

    单表查询: 数据查询命令:select 识别要查询的列 from识别要查询的表 select 运算符: + .-.*./. 加减乘除 等于= 不等于!= 或 <> 大于等于>= 小于 ...

  6. mysql——单表查询——聚合函数——概念

    使用聚合函数查询 group by关键字通常和聚合函数一起使用 .count()函数 count()函数用来统计记录的条数 举例:使用count()函数统计employee表的记录数 select c ...

  7. Mysql 在 select 查询时追加(添加)一个字段并指定值

    在特定时候,在 mysql 的查询结果中我们需要追加一个字段来实现某些特定的功能,这时我们可以用到以下语法来实现 值 as 字段比如我们需要给这个查询结果追加一个 xx 字段并赋值为 null ,可以 ...

  8. mysql——单表查询——聚合函数——示例

    ), km ), cj ) ); select * from score; ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ; 查询此同学的总成绩: ; ...

  9. Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)

    Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==== ...

随机推荐

  1. 在Visual Studio中利用NTVS创建Pomelo项目

    刚看新闻,才知道微软发布了Node.js Tools for Visual Studio(NTVS),受够了WebStorm输入法Bug的困扰,这下终于可以解脱了.以Pomelo为例,运行命令:pom ...

  2. jqgrid 设置单元格编辑/不可编辑

    首先设置不可编辑,如下代码: $(', 'not-editable-cell'); 在单元格上设置一个'not-editable-cell'就可以了,如果需要设置为可编辑,那么可以使用下面的代码: f ...

  3. tcp/udp socket编程异同

    一.TCP与UDP的区别 基于连接与无连接 对系统资源的要求(TCP较多,UDP少) UDP程序结构较简单 流模式与数据报模式 TCP保证数据正确性,UDP可能丢包 TCP保证数据顺序,UDP不保证 ...

  4. HDU5739-Fantasia(tarjan求割点)

    题意:给一个无向图n个点1~n,m条边,sigma(i*zi)%(1e9+7).zi是这个图删掉i点之后的价值.一个图的价值是所有连通子图的价值之和,连通图的价值是每个点的乘积. 题解:讲道理这题不算 ...

  5. Java 8 开发顶级技巧

    本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 我使用Java 8编码已经有些年头,既用于新的应用程序,也用来迁移现有的应用,感觉是时候写一些我发现的非常有用的 ...

  6. Java File 类的使用方法详解

    Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对Java File文件操作类进行详细地分析,并将File类中的常用方法进行简单介绍,有需要的Java开发者可以看 ...

  7. unigui多页签UI框架

    procedure TMainForm.openForm(Caption, FormClassName: string);var i: integer; sheet: TUniTabSheet;beg ...

  8. android 随手记 videoview循环播放网络视频 和mediaplayer+sufaceview播放网络视频

    1:videoview循环播放视频 1>xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...

  9. Partition Array

    Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...

  10. 【Stage3D学习笔记续】山寨Starling(九):上下文丢失处理方法

    Stage3D在运行中是存在随时会丢失上下文的尴尬情况. 渲染内容丢失的问题本身就说明是因为丢失了Context3D对象.出现此问题的原因很多,通常还不是因为Stage3D应用.比如在win7系统中, ...