MYsql增删改查

笔记1:http://www.cnblogs.com/hoojo/archive/2011/06/20/2085390.html

笔记2:http://www.cnblogs.com/hoojo/archive/2011/06/20/2085416.html

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