1 ## 2 ## 3 ## 4 ## 文件包含: 数据的增删改查 5 ## 6 ## 其中包含: 数据的: 7 ## 1.第一部分增删改 8 ## 2.第二部分为查询语句 9 ## 3.第三部分为多表查询(交叉连接,内连接,外链接和自然连接)★★★★★★★ 10 ## 11 ## 12 ## // +---------------------------------------------------------------------- 13 ## // | 数据操作 14 ## // | 1.增------------增加数据 15 ## // | 2.删------------删除数据 16 ## // | 3.改------------修改数据(更新) 17 ## // +----------------------------------------------------------------------# 18 ## 19 ## 20 ## 21 #1.数据增加操作 22 CREATE TABLE IF NOT EXISTS `admin4`( 23 `adminid` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID', 24 `adminuser1` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '管理员账号', 25 `adminpass` CHAR(32) NOT NULL DEFAULT '' COMMENT '管理员密码', 26 `adminage` CHAR(32) NOT NULL DEFAULT '' COMMENT '管理员密码', 27 PRIMARY KEY(`adminid`) 28 )ENGINE=InnoDB DEFAULT CHARSET=utf8; 29 30 INSERT INTO `admin4`(adminuser1,adminpass,adminage) 31 VALUES 32 ('zhang1',md5('admin'),'12'), 33 ('zhang2',md5('admin'),'24'), 34 ('zhang3',md5('admin1'),'24'), 35 ('zhang4',md5('admin'),'34'); 36 #2.数据删除操作 37 delete from `admin` where adminid = 3; 38 truncate `admin`; 39 #3.数据修改更新操作 40 update `admin` set adminuser = 'liuhaiqiang' where adminid = 3; 41 ## 42 ## 43 ## 44 ## // +---------------------------------------------------------------------- 45 ## // | 数据查询操作 46 ## // | 1.------------where 条件语句 47 ## // | 2.------------group by 分组语句 48 ## // | 3.------------having 对分组条件查询语句 49 ## // | 4.------------order by 对数据的排序 50 ## // | 5.------------limit 1,2 对数据截取,用于分页 51 ## // +----------------------------------------------------------------------# 52 ## 53 ## 54 ## 55 1--where 语句 56 select * from admin where id=1; 57 2--group by语句 统计分组 58 select adminuser,adminage,count(*), max(adminage),min(adminage) 59 from admin 60 where adminuser = 'admin' 61 group by adminuser; 62 3--having 语句 对统计分组的数据再次分组追加条件 63 select adminuser,adminage,count(*), max(adminage),min(adminage) 64 from admin 65 where adminuser = 'admin' 66 group by adminuser having count(*)>=2; 67 4--order by 语句 desc 和 asc 数据指定字段的升序或者降序 68 select adminuser,adminage,count(*), max(adminage),min(adminage) 69 from admin 70 where adminuser = 'admin' 71 group by adminuser having count(*)>=0 order by adminage asc; 72 5--limit 1,2 数据截取 多用于分页 73 select adminuser,adminage,count(*), max(adminage),min(adminage) 74 from admin 75 where adminuser = 'admin' 76 group by adminuser having count(*)>=0 order by adminage asc limit 1,2; 77 ## 78 ## 79 ## 80 ## // ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 81 ## // ★ 多表查询 82 ## // ★ 1.交叉连接------------cross jion 83 ## // ★ 2.内连接--------------inner jion 84 ## // ★ 3.左连接--------------left jion 85 ## // ★ 4.右连接--------------right jion 86 ## // ★ 5.自然连接------------limit 1,2 对数据截取,用于分页 87 ## // ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 88 ## 89 ## 90 ## 91 --1.交叉连接 无卵用 数据的乘积 如4*4 = 16 92 select * from admin cross join admin2; 93 --2.内连接 on 可以替换成 where (where查询速度快) 94 select * from admin inner join admin2 on admin.adminuser=admin2.adminuser 95 where admin.adminid=2 and admin2.adminid=2; 96 select admin.*,admin2.adminage from admin inner join admin2 where admin.adminuser=admin2.adminuser 97 and admin.adminid=2 and admin2.adminid=2;||having adminid = 2; 98 --2.左连接 用左边匹配右边的表,条件中的语句再无法和左边的进行匹配则数据为空 99 select s.*,c.adminuser 100 from admin as s left join admin3 as c on 101 s.adminid=c.adminid having adminid = 3; 102 --3.右连接 103 select s.*,c.adminuser 104 from admin as s right join admin3 as c on 105 s.adminid=c.adminid having adminid = 3; 106 --4.自然连接(自动过滤掉重复的字段名,当字段名不同时显示 右表中不同字段的数据) 107 select * from admin natural join admin3;