mysql数据库高级应用第三章.ppt_第1页
mysql数据库高级应用第三章.ppt_第2页
mysql数据库高级应用第三章.ppt_第3页
mysql数据库高级应用第三章.ppt_第4页
mysql数据库高级应用第三章.ppt_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MySQL查询语句们 查询语句的基本格式: select 列名1,列名2 from 表名 where where_definition group by unsigned_integer | col_name | formula ASC | DESC, . having where_definition order by unsigned_integer | col_name | formula | DESC ,. limit offset, rows | rows OFFSET offset ASC 说明: 1、查旬所有列:可用*来表示: select * from news_table; 2、中的为可选语句。 where 条件,用来筛选数据。 group by 用来分类汇总。 having :用来筛选分类汇总的结果。 order by用来排序。 limit用来取出指定的记录数。 根据sql查询表的个数,可以分为单表查询 和多表查询。 单表查询 1、创建stu_info表 create table stu_info ( id int auto_increment, stu_num varchar(20), stu_name varchar(20), stu_sex char(2), stu_age int, stu_grade varchar(20), stu_class varchar(20), stu_subject varchar(20), stu_fee decimal(6,2), stu_time datetime, primary key(id) ); 字段说明如下: id,学号,姓名,性别,年龄,年级,班级,科目,成绩,时间 Code:code/create_stu_info.txt 2、查询所有数据 格式: select * from 表名; 例子: Select * from stu_info; 查询这个表的所有列(字段) Select id,stu_name from stu_info; 查询这个表的所有数据,只显示id和stu_name两个列。 指定别名: select id as 序号 from stu_info; select id 序号 from stu_info; select id as 序号,stu_name as 姓名 from stu_info; 指定别名,只是为了显示更加真观,并不真正改变表的列名。 3、where语句 where语句,后面可以跟着多个条件,从而来限制查询的数据。多个条件之间,可以用 and 或者 or 来链接。 例如: Select * from stu_info where id=5; Select * from stu_info where id5; Select * from stu_info where id5; 分别表示: id为5的数据;id大于5的数据;Id小于或者等于5的数据;id不等于的数据; Select * from stu_info where id2 and stu_name=张三; Select * from stu_info where id2 or stu_name2 limit 5; 前5条记录 select * from stu_info where id2 limit 5,10; 第5条记录之后的前10条记录。 Code:code/mysql 4.txt 8、like及in关键字 like表示相似,比如我们想查询姓名中包括姓“张”的学生名单,新闻标题中包含“中国”的新闻 。这也就通用所说的“模糊查询”。 用法: select * from 表名 where 字段 like 字符及通配符; 例如: select * from stu_info where id2 and stu_name like 张; select * from stu_info where stu_name like 张%; select * from stu_info where stu_name like %张%; in 表示括号列表中是否包含的此信息。 select * from stu_info where id in (1,2,3);表示查询id为1,2,或3的记录。 select * from stu_info where stu_name in (张三,李四); 注意括号中值的写法:多个值之间以逗号分隔,字符类型用单引号括起。 多表查询及子查询 9、表的别名 查询也可以这样: select 列名1,列名2 from 表名; 它的完整写法是: select 表名.列名1,表名.列名2 from 数据库名.表名; 在实际应用中,我们一般省略字段前面的表名,表名前面的数据库名。 和字段的别名一样,表也可以指定别名: select 别名.列名 from 表名 别名; 那 么: select * from news; 等效的写法还有: select news.* from news; select n.* from news n; select n.id,n.title from news n; 10、多表查询 一般情况下,我们查询一个表就能满足业务需求。但经常地,我们也希望同时来查询多个表,来得 到数据集。 姓名年龄 张三23 李四22 姓名奖金 张三232.22 李四5000.22 王五 如上图,现在想要得到每个人的姓名,年龄,奖金明细,显示对一个表的查询满足不了需求。这就 需要多表查询。 语法: select 字段1,字段2 from 表A,表B where 条件 等 例如: 创建多表code: :code/创建多表查询.txt select ,a.age,,b.money from employee_info a,employee_pay b 11、多表查询 执行 code/创建多表查询.txt 代码,创建:employee_info员工表 employee_pay 工资表 笛卡尔积: select a.*,b.* from employee_info a,employee_pay b; 直接对两个表查询,不附加任何条件,将得到笛卡尔积。数据条数为 A表记录数 x B表记录数。 即A表的每一条记录分别与B表的第一条记录组合,与B表的第二条数据组合。 等值连接:显示的A、B两表均符合条件的记录。 写法1:select a.*,b.* from employee_info a,employee_pay b where =; 写法2:select a.*,b.* from employee_info a join employee_pay b on =; 左连接:left join 显示A表所有记录,及B表符合条件的记录。 select a.*,b.* from employee_info a left join employee_pay b on =; 右连接:right join 显示B表所有记录,及A表符合条件的记录。 select a.*,b.* from employee_info a right join employee_pay b on =; 用join连接表,用on指定条件。join on 也是SQL1999的标准,受到所有主流数据库的支持。 Code:code/多表查询示例.txt 12、自查询与子查询 -自查询 自查询是一种特殊的多表查询。如: select a.*,b.* from train a,train b where a.idb.id; -单行子查询: 可用 (select id from sb where trim(name)=中国) select * from sa where id=(select id from sb where trim(name)=中国) -多行子查询: 可用in,not in来连接。 select * from sa where id in (select id from sb); select * from sa where id not in(1,2,3,4); 13、常用查询命令 SELECT VERSION(), NOW(); 查看MySQL的版本,当前时间。 Select user(); 查看当前用户。 c 表示取消上述命令。 14、应用:站站查询 应用:设计一个列车时刻表,实现车次查询、站站查询。 表结构如下: Field Type Collation Null Key Default Extra Privileges Comment - - - - - - - - - id int(11) NULL PRI (NULL) auto_increment select,insert,update,references t_num varchar(20) latin1_swedish_ci YES (NULL) select,insert,update,references t_station varchar(20) latin1_swedish_ci YES (NULL) select,insert,update,references t_time datetime NULL YES (NULL) select,insert,upda

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论