基础查询和数据库的调用
2014-12-16 20:53
253 查看
欢迎来到unity学习、unity培训
这里有很多U3D资源、U3D培训视频、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌
什么是连接查询呢?
概念:根据两个表或多个表的列之间的关系,从这些表中查询数据。
目的:实现多个表查询操作。
知道了连接查询的概念之后,什么时候用连接查询呢?
一般是用作关联两张或两张以上的数据表时用的。看起来有点抽象,我们举个例子,做两张表:学生表(T_student)和班级表(T_class)
1、内连接查询(跟表的位置无关)
select *fron table1 join table2 on table1.条件列名 = table2.条件列名
select u.name,s.grade from score as s inner join users as u on s.uid=u.id
2、左外连接查询(跟表的位置有关)
select * from table1 left join table2 on table1.条件列名 = table2.条件列名
select u.name,s.grade from score as s left join users as u on s.uid=u.id
3、右外连接查询(跟表的位置有关)
select *from table1 right join table2 on table1. 条件列= table2.条件列
select u.name,s.grade from score as s right join users as u on s.uid=u.id
4、like模糊查询
SELECT 字段 FROM 表 WHERE 某字段 Like 条件
其中关于条件,SQL提供了四种匹配模式:
1、% :表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
2、_ : 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:
3、[ ] :表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
4、[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
select id,name from users where name like '%a%'
5、Between...and
把某一字段中内容在特定范围内的记录查询出来
select id,name from users where id between 1 and 2
select id,name from users where id >=1 and id<=2
6、In
把某一字段中内容与所列出的查询内容列表匹配的记录查询出来
select id,name from users where id in(1,2,3)
聚合查询
定义:一种查询(SQL语句),它通过包含一个聚合函数(如Sum或Avg)来汇总来自多个行的信息。
4000
7、sum avg
Mum:求和 avg:平均值
select sum(id) as 总和 from users
select avg(id) as 平均 from users
8、max min
Max:最大 min:最小
select max(id) as 最大,min(id) as 最小 from users
9、Count
select count(*) as 总人数 from users
select count(password) as 总人数 from users
10、分组
select id,avg(id) as 平均 from users group by id
select id,avg(id) as 平均 from users group by id,name
这里有很多U3D资源、U3D培训视频、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌
什么是连接查询呢?
概念:根据两个表或多个表的列之间的关系,从这些表中查询数据。
目的:实现多个表查询操作。
知道了连接查询的概念之后,什么时候用连接查询呢?
一般是用作关联两张或两张以上的数据表时用的。看起来有点抽象,我们举个例子,做两张表:学生表(T_student)和班级表(T_class)
1、内连接查询(跟表的位置无关)
select *fron table1 join table2 on table1.条件列名 = table2.条件列名
select u.name,s.grade from score as s inner join users as u on s.uid=u.id
2、左外连接查询(跟表的位置有关)
select * from table1 left join table2 on table1.条件列名 = table2.条件列名
select u.name,s.grade from score as s left join users as u on s.uid=u.id
3、右外连接查询(跟表的位置有关)
select *from table1 right join table2 on table1. 条件列= table2.条件列
select u.name,s.grade from score as s right join users as u on s.uid=u.id
4、like模糊查询
SELECT 字段 FROM 表 WHERE 某字段 Like 条件
其中关于条件,SQL提供了四种匹配模式:
1、% :表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
2、_ : 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:
3、[ ] :表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
4、[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
select id,name from users where name like '%a%'
5、Between...and
把某一字段中内容在特定范围内的记录查询出来
select id,name from users where id between 1 and 2
select id,name from users where id >=1 and id<=2
6、In
把某一字段中内容与所列出的查询内容列表匹配的记录查询出来
select id,name from users where id in(1,2,3)
聚合查询
定义:一种查询(SQL语句),它通过包含一个聚合函数(如Sum或Avg)来汇总来自多个行的信息。
4000
7、sum avg
Mum:求和 avg:平均值
select sum(id) as 总和 from users
select avg(id) as 平均 from users
8、max min
Max:最大 min:最小
select max(id) as 最大,min(id) as 最小 from users
9、Count
select count(*) as 总人数 from users
select count(password) as 总人数 from users
10、分组
select id,avg(id) as 平均 from users group by id
select id,avg(id) as 平均 from users group by id,name
相关文章推荐
- 基础查询和数据库的调用
- SQL点滴17—使用数据库引擎存储过程,系统视图查询,DBA,BI开发人员必备基础知识
- Hibernate HQL基础 调用数据库存储过程
- 关系型数据库基础之:简单的数据查询
- mongodb基础系列——数据库查询数据返回前台JSP(二)
- mongodb基础系列——数据库查询数据返回前台JSP(一)
- 数据库连接,有时会出错在conn.open connstr这地方的问题,及联接查询调用rs("user.id")的问题2006-6-8-4
- 关系代数是关系数据库系统查询语言的理论基础
- MSSQLSERVER数据库- C#里调用存储过程,多参数查询,个人记录
- 数据库学习笔记(四)-数据库查询基础
- 数据库学习笔记---SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)
- SQL 数据库查询出来的数据集调用储存过程删除右边多余的0
- Oracle 10g数据库基础之基本查询语句-上
- Hibernate HQL基础 调用数据库存储过程
- 关系型数据库基础之:简单的数据查询
- ObjectARX中调用ADO封装类实现数据库查询读取写入数据的一个类实例
- ObjectARX中调用ADO封装类实现数据库查询读取写入数据的一个类实例
- Hibernate HQL基础 调用数据库存储过程
- mongodb基础系列——数据库查询数据返回前台JSP(一)
- SQL使用数据库引擎存储过程,系统视图查询,DBA,BI开发人员必备基础知识