?重点?
//由于windows的命令行字符集是gbk为了能够在命令行正确输出汉字,这里需要用 set names gbk;(不建议这种设置字符集,可能出现注入等问题)
//主要在做国内站的时候考虑这种,国外的直接可以set names这种设置方式,国内站建议:
set character set connection =gbk,
character_set_result = gbk,
character_set_client = binary;
?重点?
查看服务器支持哪些字符集:show character set
查看字符集校对规则:show collaction
查看系统字符集设置变量:show variables like '%character%'
//1.创建学生表(主键id,姓名、性别、年龄、爱好)
//先连接数据库
mysql -uroot -p
root
//创建数据库
create database info charset utf8;(这里的utf8指数据库的字符集编码)
//设置命令行字符集
set names gbk;
//建立学生表
create table student(id primary key auto_increment,
stu_name varchar(20) not null default '',
sex enum('男','女') not null default '男',
age tinyint not null default 0,
hobby set('足球','篮球','羽毛球','橄榄球','排球') not null default '足球,羽毛球');
//2.创建学生日记表diary(主键id、标题、内容、作者、点击次数)
//content text
//create table diary(id primary key auto_increment,
title varchar(20) unique not null default '',
content text,
author varchar(10) not null default '',
clicks int not null default 0);
#知识点
##1.primary key(主键),2.auto_increment(自动增长)连着用
##(一个表有且只有一个主键)
##3.unique(唯一标识)
###补充:zerofill(补充无意义的前导0)该补充位数根据数值型后括号后跟的数字,
例如:
create table ss(age tinyint(5));
如果插入数据age为18这插入到数据表中的结构就是00018
##4.order by(排序),后面跟字段,默认升序(从小到大)
desc降序(从大到小)asc升序(一般不写)
##5.as 别名 由于数据表中可能有很长的名字,别名可以让程序员更清晰快速了解该项内容
写法:
1.select auther_vip_name as name from 表名
2.select auther_vip_name name from 表名(as可以省略)
6.like 模糊查询,通常和%或者_使用
例如:
select * from 表名 where name='杨%';
查找姓杨的同学
select * from 表名 where name='杨_';
查找姓杨且只有两个字
解析:%可以代表任意个字符,_代表一个字符
7.rand() 和 order by 一起使用代表随机数据
select * from 表名 order by rand() limit 6;
随机截取6条数据limit截取
补充limit:
limit 后更一个数字代表截取几个;
limit(2,5) 表示从下标为2的数据开始截取5条
8.left,mid,right
select left(name,2) from 表名;
搜索表名数据表中name字段显示前两个字符
select right(name,2) from 表名;
搜索表名数据表中name字段显示后两个字符
select mid(name,2,2) from 表名;
搜索表名数据表中name字段显示第二个字符和第三个字符这两个字符
9.in()(not in())在什么中
select age from 表名 where age in(18,28,8);
搜索年龄是18,28,8的数据
10.betwwen A and B 在什么之间
select age from 表名 where age betwwen 18 and 58;
搜索年龄在18-58之间的数据
11.and表交集,or表并集
12.distinct过滤重复数据distinct用于去重比如说统计用户的购物种类
13.concat合并字段没什么大用
select concat(name,age) from 表名;
当然中间可以加|等隔离
出来的数据
name,age
陈,18
杨,18
//1.删除年龄最大的一个女同学
delete from data where sex='女' order by age desc limit 1;
//2.将年龄大于20岁的同学,年龄全部+1
update data set age=age+1 where age>20;
(情景数据表为data,有女同学存在)
?????
数据表的增删改查
(情景是学生表student)
1.数据表数据的增加
(1)insert into student set name='';
(2)insert into student () values (),()...;
@说明replace,可以增加可以替换(替换后如果没赋值则走默认)而且要考虑什么时候替换什么时候插入,不建议使用(当主键相同时替换)
2.数据表数据的删除
delete from 数据表 where 条件
delete from student where id=1;
3.数据表数据的更新(改)
update 表名 set 字段=值 where 条件
update student set age=age+1 where age>18;
4数据表数据的查询
select * from 表名 where 条件;
//其中*可以换成其他字段,可以不要条件
//查找年龄最小的同学(birthday)
select * from 表名 order by birthday desc limit 1;
//查找所有1990之后的同学
select * from 表名 where birthday>'1900-00-00';
//获得标签数量最多的一篇文章
select 文章字段名,count(*) 统计别名(total) from 表名1 别名1 join 表名2 别名2 on 桥梁条件 group by 别名1.主键 order by 统计别名(total) desc limit 1;
//检索出文章【西游女儿国看不懂】的标签数量
select 文章字段名,count(*) 统计别名(total) from 表名1 别名1 join 表名2 别名2 on 桥梁条件 where a.atitle='西游女儿国看不懂';
//检索出每篇文章所对应的标签
select 别名1.文章字段名,别名3.标签字段名 from 表名1 别名1 join 表名2 别名2 on 桥梁条件 join tag t on at.tid=t.tid;
//检索出文章的标签id
select 文章字段名,id字段名 from 表名1 别名1 join 表名2 别名2 on 桥梁条件 group by 别名1.主键 order by at.aid desc;
//例子大全;
//环境一,一对多
//班级表class
cid cname
1c89
2c90
3c91
4c92
//学生表stu
sid sname cid
1 张三 1
2 李四 1
3 王五 1
4 张龙 2
5 赵虎 3
6 奥特曼 4
7 光头强 4
8 佩奇 3
环境二,多对多
//文章表article
aid atitle
1145天终于见雨水
2最近雾霾好大
3西游女儿国看不懂
4明天休息了
5应该感谢谢今天补课
//中间表(arc_tag)
aid tid
1 1
1 2
1 5
21
22
33
34
36
45
51
58
//标签tag
tid tname
1 天气
2 心情
3 八卦
4 娱乐
5 生活
6 电影
7 情感
8 教育
'创建学生表(主键id,姓名、性别、年龄、爱好)',
//create table student(
//id int primary key auto_increment,
//name char(20) not null default '',
//sex enum('男','女') not null default '男',
//age tinyint not null defualt 0
//);
'创建学生日记表diary(主键id、标题、内容、作者、点击次数)',
//create table diary(
//id int primary key auto_increment,
//title char(20) unique not null default '',
//content text ,
//actor char(5) not null default '',
//click int unsigned not null default 600
//);
'连接mysql',
//mysql -uroot -proot -h192.168.2.1
'查看所有的数据库',
//show databases;
'查看所有数据表',
//show tables;
'查找班级有哪些女生',
//select * from student where sex='女';
'检索20岁以上的女同学',
//select * from student where sex='女' and age>20;
'查询name为空的同学',
//select * from student where name is null;
'查询年龄最大的三位同学',
//select * from student order by age desc limit 3;
'查询年龄在28-78之间的同学(between)',
//select * from student where age between 28 and 78
'查找年龄在28,29,35的所有同学(in)',
//select * from student where age in (28,29,35);
'查找姓王的同学',
select * from student where name like '王%';
'查询班级(student)表所有同学的名字',
//select name from student;
'随机获取6条学生表数据',
//select *from student order by rand() limit 6;
'学生表(student)一次录入两条数据',
//insert into student (name,age,sex) values ('张三',18,'男'),('李四',28,'女');
'将学生表中id=5的数据姓名进行修改',
//update student set name='修改' where id=5;
'删除学生表中id=5的数据',
//delete from student where id=5;
'将学生表(student)重命名为(stu)',
//alter table student rename stu;
'学生表添加一个time时间字(注册时间)',
//alter table stu add time int not null default 0;
'学生表添加一个birthday(生日类型)',
//alter table stu add birthday date not null default 0;
'删除字段time',
//alter table student drop time;
'将student表name字段修改为varchar(200)',
//alter table student modify name varchar(200) not null default '';
'删除学生表主键自增',
//alter table student modify id int;
//alter table student drop primary key;
'给学生表添加主键自增',
//alter table student add primary key(id);
//alter table student modify id int auto_increment;
'删除年龄最大的一个女同学',
//delete from student where sex='女' order by age desc limit 1
'将年龄大于20岁的同学,年龄全部+1',
//update student set age=age+1 where age>20;
'查找年龄最小的同学(birthday)',
//select * from student order by birthday desc limit 1;
'查找所有1990之后的同学',
//select * from student where year(birthday) >=1990;
'获得标签数量最多的一篇文章',
//select a.atitle,count(*) from article a join arc_tag at on a.aid=at.tid group by a.atitle order by count(*) desc limit 1;
'检索出每篇文章所对应的标签',
//select a.atitle,t.tname from article a join arc_tag at on a.aid=at.aid join tag t on at.tid=t.tid;
'检索出文章【西游女儿国看不懂】的标签数量',
//select a.atitle,count(*) from article a join arc_tag at on a.aid=at.aid where a.atitle='西游女儿国看不懂';
'张三的同班同学',
//select s2.sname from stu s1 join stu s2 on s1.cid=s2.cid where s1.sname='张三' and s2.sname!='张三';
'统计每个文章有几个标签'
//select a.atitle , count(*) from article a join arc_tag at on a.aid=at.aid group by a.aid;
];
//pdo
封装类Model将连接数据库和语句进行简易的封装
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/19
* Time: 13:56
*/
include "./helper.php";
class Model{
//连接数据库,用一个标来判断是否已经连接了数据库
private static $pdo = null;
public function __construct ()
{
if (is_null (self::$pdo)){
//抛出异常
try{
//1.连接数据库
$dsn = 'mysql:host=127.0.0.1;dbname=homework';
self::$pdo = new PDO($dsn,'root','root');
//2.设置字符集
//set character set connection =gbk,
//character_set_result = gbk,
//character_set_client = binary;
self::$pdo->query ('set character set connection=gbk,character_set_result=gbk,character_set_client=binary');
//设置错误信息
self::$pdo->setAttribute (PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (Exception $e){
die($e->getMessage ());
}
}
}
/*
* 当有结果集即返回时
*/
//public function query($sql){
//$res = self::$pdo->query ($sql);
//return $res->fetchAll (PDO::FETCH_ASSOC);
//}
/*
* 当没有结果集返回时
*/
//public function exec($sql){
//return $res = self::$pdo->exec ($sql);
//}
public function sqldb($sql){
try{
//判断截取的字符串是否为select如果是证明有结果集实行query
if(substr ($sql,0,6)=='select'){
$res = self::$pdo->query ($sql);
return $res->fetchAll (PDO::FETCH_ASSOC);
}else{
//否则执行没有结果集的exec
return $res = self::$pdo->exec ($sql);
}
self::$pdo->setAttribute (PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (Exception $exception){
die($exception->getMessage ());
}
}
}
//实例化打印看结果
//$result = (new Model())->query ('select * from article');
//(new Model())->exec ('insert into article(atitle) values("今天")');
//(new Model())->exec ('delete from article where atitle="今天"');
$result = (new Model())->sqldb('select * from article');
//$result = (new Model())->sqldb('insert into article(atitle) values("今天")');
//$result = (new Model())->sqldb ('delete from article where atitle="今天"');
dd ($result);
一键复制
编辑
Web IDE
原始数据
按行查看
历史