您的位置:首页 > 数据库

基础查询和数据库的调用

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息