mysql操作数据库语句整理(方便以后使用)
2014-05-12 15:38
671 查看
1 :any的用法
表结构如下:(如果表没有关系,这个也就没用处)
班级表:
学生表
=any相当于in语句 相等的意思
select * from classstu where class_Id=any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 1 | 1232 |
| 2 | 一年级 |
+----------+------------+
2 rows in set
>any 大于any的意思就是大于等于最大的
select * from classstu where class_Id>any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 2 | 一年级 |
| 3 | 二年级 |
+----------+------------+
2 rows in set
mysql>
<any的意思小于等于最小的包含的意思
select * from classstu where class_Id<any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 1 | 1232 |
+----------+------------+
1 row in set
第二点:join的用法
inner join 内连接 跟join的用法类似.将多张表符合条件的都列出来
select * from classstu join student using(class_Id);
+----------+------------+-----------+---------+-----+
| class_Id | class_name | studentID | name | age |
+----------+------------+-----------+---------+-----+
| 1 | 1232 | 1 | 一班059 | 25 |
| 1 | 1232 | 2 | 一班059 | 25 |
| 1 | 1232 | 3 | 一班059 | 25 |
| 1 | 1232 | 4 | 一班059 | 25 |
| 1 | 1232 | 5 | 一班059 | 25 |
| 2 | 一年级 | 6 | 二班059 | 25 |
| 2 | 一年级 | 7 | 二班059 | 25 |
| 2 | 一年级 | 8 | 二班059 | 25 |
+----------+------------+-----------+---------+-----+
2:left join on的用法
select * from classstu left
join student using(class_Id);
+----------+------------+-----------+---------+------+
| class_Id | class_name | studentID | name | age |
+----------+------------+-----------+---------+------+
| 1 | 1232 | 1 | 一班059 | 25 |
| 1 | 1232 | 2 | 一班059 | 25 |
| 1 | 1232 | 3 | 一班059 | 25 |
| 1 | 1232 | 4 | 一班059 | 25 |
| 1 | 1232 | 5 | 一班059 | 25 |
| 2 | 一年级 | 6 | 二班059 | 25 |
| 2 | 一年级 | 7 | 二班059 | 25 |
| 2 | 一年级 | 8 | 二班059 | 25 |
| 3 | 二年级 | NULL | NULL | NULL |
他的意思是将 前者的表中的数据全部列出 后者必须满足条件
3 right join on的用法 是依赖于后者. 必须满足后者的条件
select * from classstu right
join student using(class_Id);
+----------+-----------+---------+-----+------------+
| class_id | studentID | name | age | class_name |
+----------+-----------+---------+-----+------------+
| 1 | 1 | 一班059 | 25 | 1232 |
| 1 | 2 | 一班059 | 25 | 1232 |
| 1 | 3 | 一班059 | 25 | 1232 |
| 1 | 4 | 一班059 | 25 | 1232 |
| 1 | 5 | 一班059 | 25 | 1232 |
| 2 | 6 | 二班059 | 25 | 一年级 |
| 2 | 7 | 二班059 | 25 | 一年级 |
| 2 | 8 | 二班059 | 25 | 一年级 |
+----------+-----------+---------+-----+------------+
8 rows in set
表结构如下:(如果表没有关系,这个也就没用处)
班级表:
学生表
=any相当于in语句 相等的意思
select * from classstu where class_Id=any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 1 | 1232 |
| 2 | 一年级 |
+----------+------------+
2 rows in set
>any 大于any的意思就是大于等于最大的
select * from classstu where class_Id>any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 2 | 一年级 |
| 3 | 二年级 |
+----------+------------+
2 rows in set
mysql>
<any的意思小于等于最小的包含的意思
select * from classstu where class_Id<any(select class_id from student);
+----------+------------+
| class_Id | class_name |
+----------+------------+
| 1 | 1232 |
+----------+------------+
1 row in set
第二点:join的用法
inner join 内连接 跟join的用法类似.将多张表符合条件的都列出来
select * from classstu join student using(class_Id);
+----------+------------+-----------+---------+-----+
| class_Id | class_name | studentID | name | age |
+----------+------------+-----------+---------+-----+
| 1 | 1232 | 1 | 一班059 | 25 |
| 1 | 1232 | 2 | 一班059 | 25 |
| 1 | 1232 | 3 | 一班059 | 25 |
| 1 | 1232 | 4 | 一班059 | 25 |
| 1 | 1232 | 5 | 一班059 | 25 |
| 2 | 一年级 | 6 | 二班059 | 25 |
| 2 | 一年级 | 7 | 二班059 | 25 |
| 2 | 一年级 | 8 | 二班059 | 25 |
+----------+------------+-----------+---------+-----+
2:left join on的用法
select * from classstu left
join student using(class_Id);
+----------+------------+-----------+---------+------+
| class_Id | class_name | studentID | name | age |
+----------+------------+-----------+---------+------+
| 1 | 1232 | 1 | 一班059 | 25 |
| 1 | 1232 | 2 | 一班059 | 25 |
| 1 | 1232 | 3 | 一班059 | 25 |
| 1 | 1232 | 4 | 一班059 | 25 |
| 1 | 1232 | 5 | 一班059 | 25 |
| 2 | 一年级 | 6 | 二班059 | 25 |
| 2 | 一年级 | 7 | 二班059 | 25 |
| 2 | 一年级 | 8 | 二班059 | 25 |
| 3 | 二年级 | NULL | NULL | NULL |
他的意思是将 前者的表中的数据全部列出 后者必须满足条件
3 right join on的用法 是依赖于后者. 必须满足后者的条件
select * from classstu right
join student using(class_Id);
+----------+-----------+---------+-----+------------+
| class_id | studentID | name | age | class_name |
+----------+-----------+---------+-----+------------+
| 1 | 1 | 一班059 | 25 | 1232 |
| 1 | 2 | 一班059 | 25 | 1232 |
| 1 | 3 | 一班059 | 25 | 1232 |
| 1 | 4 | 一班059 | 25 | 1232 |
| 1 | 5 | 一班059 | 25 | 1232 |
| 2 | 6 | 二班059 | 25 | 一年级 |
| 2 | 7 | 二班059 | 25 | 一年级 |
| 2 | 8 | 二班059 | 25 | 一年级 |
+----------+-----------+---------+-----+------------+
8 rows in set
相关文章推荐
- mysql乱码
- percona-toolkit
- Mysql学习-(4-创建数据表)
- mysql查看所有存储过程
- mysql存储过程的调用方法
- Mysql学习-(3-MySQL数据库管理系统概述)
- mac上安装MySQL
- 提高MySQL效率与性能的技巧
- 访问远程mysql数据库
- mysql
- mySql数据库外键约束条件
- Mysql学习-(2- 十步完全理解SQL)
- MySQL导入.sql文件及常用命令
- MAMP中设置Mysql root密码
- mysql中engine=innodb和engine=myisam的区别
- mysql默认安装的四个数据库
- mysql 给表添加一个联合唯一索引
- mysqlsla慢查询分析工具使用笔记
- 查看修改mysql编码方式
- MySQL Memo