《mysql数据库高级应用第三章.ppt》由会员分享,可在线阅读,更多相关《mysql数据库高级应用第三章.ppt(17页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、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 条件,用来
2、筛选数据。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 varcha
3、r(20),stu_fee decimal(6,2),stu_time datetime,primary key(id);字段说明如下:id,学号,姓名,性别,年龄,年级,班级,科目,成绩,时间Code:code/create_stu_info.txt2、查询所有数据、查询所有数据格式:格式:select*from 表名;例子:例子:Select*from stu_info;查询这个表的所有列(字段)Select id,stu_name from stu_info;查询这个表的所有数据,只显示id和stu_name两个列。指定别名:select id as 序号 from stu_info;s
4、elect 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 id=5;Select*from stu_info wehre id5;分别表示:id
5、为5的数据;id大于5的数据;Id小于或者等于5的数据;id不等于的数据;Select*from stu_info where id2 and stu_name=张三;Select*from stu_info where id2 or stu_name张三;用括号来指定条件执行的先后顺序:select*from stu_info where stu_grade=高一 and (stu_class=一班 or stu_class=二班);4、order by语句语句order by 用来指定数据的排序方式。有升序和降序两种。desc表示降序,asc为升序,默认为升序,asc可省略。例如:sele
6、ct*from stu_info order by id asc;按照id升序排序,其中asc可省略。select*from stu_info order by id desc;按照id降序select*from stu_info where id=5 order by stu_name;按姓名升序select*from stu_info where id20;7、limit 语句语句limit 取得某一范围的记录集。如前10条,第20到第80条。此函数常用于显示最新10条记录,数据分页等。用法:用法:select*from limit 起始位置,长度;select*from limit 长度
7、;limit写在查询语句的最后位置上。例如:例如:select*from stu_info where id2 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
8、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
9、select 列名列名1,1,列名列名2 from 2 from 表名表名;它的完整写法是:select select 表名表名.列名列名1,1,表名表名.列名列名2 from 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、
10、多表查询、多表查询一般情况下,我们查询一个表就能满足业务需求。但经常地,我们也希望同时来查询多个表,来得到数据集。姓名年龄张三23李四22姓名奖金张三232.22李四5000.22王五如上图,现在想要得到每个人的姓名,年龄,奖金明细,显示对一个表的查询满足不了需求。这就需要多表查询。语法:语法:select 字段1,字段2 from 表A,表B where 条件 等例如:创建多表code::code/创建多表查询.txtselect a.name,a.age,b.name,b.money from employee_info a,employee_pay b11、多表查询、多表查询执行 cod
11、e/创建多表查询.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 a.name=b.name;写法2:sel
12、ect a.*,b.*from employee_info a join employee_pay b on a.name=b.name;左连接:左连接:left join 显示A表所有记录,及B表符合条件的记录。select a.*,b.*from employee_info a left join employee_pay b on a.name=b.name;右连接:右连接:right join 显示B表所有记录,及A表符合条件的记录。select a.*,b.*from employee_info a right join employee_pay b on a.name=b.name
13、;用join连接表,用on指定条件。join on 也是SQL1999的标准,受到所有主流数据库的支持。Code:code/多表查询示例.txt12、自查询与子查询、自查询与子查询-自查询自查询自查询是一种特殊的多表查询。如: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,no
14、t 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 P
15、rivileges 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,update,references t_order int(11)NULL YES (NULL)select,insert,update,references t_num表示车次,如T238t_station表示列车经过站名,如哈尔滨t_time 表示到站时间t_order 表示列车经过顺序,起来站为1,第二站为2,依此类推。这样的设计,也就是说每一站,要写入一条数据。Code:code/列车站站查询.txt
限制150内