mysql 基本语句范例
2016-05-10 16:31
681 查看
选择:select * from table where 范围
创建索引:alter table users add index name(username);
插入:insert into table(field1,field2) values(value1,value2)
删除:delete from table where 范围
更新:update table set field1=value1 where 范围
查找:select * from table where field1 like ’%value1%’
排序:select * from table order by field1,field2 [desc]
总数:select count as totalcount from table
求和:select sum(field1) as sumvalue from table
平均:select avg(field1) as avgvalue from table
最大:select max(field1) as maxvalue from table
最小:select min(field1) as minvalue from table
select * from students group by sex having degree >60 order by degree limit 100,200;
从 其他表插入:insert sucess(name) select name from students where degree >60;
例:
SELECT TOP 20 * FROM table
意思是:从table中查询前 20 项的全部内容
*可以用table中的内容表示
意思是:从table中查询前 20 项”某一项”的内容
SELECT SUM(grade) as total,average(grade) as average FROM table GROUP BY class
意思是:以班级为单位,查询这个班的总成绩和平均成绩
UPDATE table SET age=age+10
意思是:将 table 中所有人的年龄增加10岁
自连接、外连接和自连接查询
表1:teacher
表2:student
内连接:在每个表中找出符合条件的共有记录。[x inner join y on…]
第一种写法:(相当于多表查询,用的是where!)
select t.,s. from teacher t,student s where t.id=s.tea_id;
第二种写法:(inner省略了)
select t.,s. from teacher t join student s on t.id=s.tea_id;
第三种写法:
select t.,s. from teacher t inner join student s on t.id=s.tea_id;
外连接有三种方式:左连接,右连接和全连接。
1 左连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配
找不到与左表匹配的,用null表示。[x left [outer] join y on…]
第一种写法:
select t.,s. from teacher t left join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t left outer join student s on t.id=s.tea_id;
找不到匹配的,用null填充。[x right [outer] join y on…]
第一种写法:
select t.,s. from teacher t right join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t right outer join student s on t.id=s.tea_id;
2.3 全连接:返回符合条件的所有表的记录,没有与之匹配的,用null表示(结果是左连接和右连接的并集)
第一种写法:
select t.,s. from teacher t full join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t full outer join student s on t.id=s.tea_id;
3 交叉连接(结果是笛卡尔积)
select t.,s. from teacher t cross join student s;
等效于:
select t.,s. from teacher t,student s;
5自连接:连接的表都是同一个表,同样可以由内连接,外连接各种组合
方式,按实际应用去组合。
SELECT a.*,b.* FROM table_1 a,table_1 b WHERE a.[name]=b.[name] –连接的两表是同一个表,别称不一样
SELECT a.*,b.* FROM table_1 a LEFT JOIN table_1 b ON a.[name]=b.[name] –左连接写法
16. in 查询
要注意字段类型,如果是数字类型用
Select * from b where s in (1,2,3,4)
如果是字符串类型用
Select * from b where s in (‘1’,’2’,’3’,’4’)
创建索引:alter table users add index name(username);
插入:insert into table(field1,field2) values(value1,value2)
删除:delete from table where 范围
更新:update table set field1=value1 where 范围
查找:select * from table where field1 like ’%value1%’
排序:select * from table order by field1,field2 [desc]
总数:select count as totalcount from table
求和:select sum(field1) as sumvalue from table
平均:select avg(field1) as avgvalue from table
最大:select max(field1) as maxvalue from table
最小:select min(field1) as minvalue from table
select * from students group by sex having degree >60 order by degree limit 100,200;
从 其他表插入:insert sucess(name) select name from students where degree >60;
例:
SELECT TOP 20 * FROM table
意思是:从table中查询前 20 项的全部内容
*可以用table中的内容表示
意思是:从table中查询前 20 项”某一项”的内容
SELECT SUM(grade) as total,average(grade) as average FROM table GROUP BY class
意思是:以班级为单位,查询这个班的总成绩和平均成绩
UPDATE table SET age=age+10
意思是:将 table 中所有人的年龄增加10岁
自连接、外连接和自连接查询
表1:teacher
id | name |
---|---|
1 | 刘德华 |
2 | 张学友 |
4 | 黎明 |
id | name | tea_id |
---|---|---|
1 | 张三 | 1 |
2 | 李四 | 1 |
3 | 王五 | 1 |
4 | 赵六 | 2 |
5 | 孙七 | 2 |
第一种写法:(相当于多表查询,用的是where!)
select t.,s. from teacher t,student s where t.id=s.tea_id;
第二种写法:(inner省略了)
select t.,s. from teacher t join student s on t.id=s.tea_id;
第三种写法:
select t.,s. from teacher t inner join student s on t.id=s.tea_id;
id | name | id | name | tea_id |
---|---|---|---|---|
1 | 刘德华 | 1 | 张三 | 1 |
1 | 刘德华 | 2 | 李四 | 1 |
1 | 刘德华 | 3 | 王五 | 1 |
2 | 张学友 | 4 | 赵六 | 2 |
2 | 张学友 | 5 | 孙七 | 2 |
1 左连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配
找不到与左表匹配的,用null表示。[x left [outer] join y on…]
第一种写法:
select t.,s. from teacher t left join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t left outer join student s on t.id=s.tea_id;
id | name | id | name | tea_id |
---|---|---|---|---|
1 | 刘德华 | 1 | 张三 | 1 |
1 | 刘德华 | 2 | 李四 | 1 |
1 | 刘德华 | 3 | 王五 | 1 |
2 | 张学友 | 4 | 赵六 | 2 |
2 | 张学友 | 5 | 孙七 | 2 |
4 | 黎明 | NULL | NULL | NULL |
2 右连接:根据右表的记录,在被连接的左表中找出符合条件的记录与之匹配,
找不到匹配的,用null填充。[x right [outer] join y on…]
第一种写法:
select t.,s. from teacher t right join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t right outer join student s on t.id=s.tea_id;
id | name | id | name | tea_id |
---|---|---|---|---|
1 | 刘德华 | 1 | 张三 | 1 |
1 | 刘德华 | 2 | 李四 | 1 |
1 | 刘德华 | 3 | 王五 | 1 |
2 | 张学友 | 4 | 赵六 | 2 |
2 | 张学友 | 5 | 孙七 | 2 |
第一种写法:
select t.,s. from teacher t full join student s on t.id=s.tea_id;
第二种写法:
select t.,s. from teacher t full outer join student s on t.id=s.tea_id;
3 交叉连接(结果是笛卡尔积)
select t.,s. from teacher t cross join student s;
等效于:
select t.,s. from teacher t,student s;
id | name | id | name | tea_id |
---|---|---|---|---|
1 | 刘德华 | 1 | 张三 | 1 |
2 | 张学友 | 1 | 张三 | 1 |
4 | 黎明 | 1 | 张三 | 1 |
1 | 刘德华 | 2 | 李四 | 1 |
2 | 张学友 | 2 | 李四 | 1 |
4 | 黎明 | 2 | 李四 | 1 |
1 | 刘德华 | 3 | 王五 | 1 |
2 | 张学友 | 3 | 王五 | 1 |
4 | 黎明 | 3 | 王五 | 1 |
1 | 刘德华 | 4 | 赵六 | 2 |
2 | 张学友 | 4 | 赵六 | 2 |
4 | 黎明 | 4 | 赵六 | 2 |
1 | 刘德华 | 5 | 孙七 | 2 |
2 | 张学友 | 5 | 孙七 | 2 |
4 | 黎明 | 5 | 孙七 | 2 |
方式,按实际应用去组合。
SELECT a.*,b.* FROM table_1 a,table_1 b WHERE a.[name]=b.[name] –连接的两表是同一个表,别称不一样
SELECT a.*,b.* FROM table_1 a LEFT JOIN table_1 b ON a.[name]=b.[name] –左连接写法
16. in 查询
要注意字段类型,如果是数字类型用
Select * from b where s in (1,2,3,4)
如果是字符串类型用
Select * from b where s in (‘1’,’2’,’3’,’4’)
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志
- MySQL 安全事宜
- MySQL 备份与恢复