mysql

-- 更新部分班级(前三个python101为java101)
update my_copy set name='java101' where
name='python101' limit 3;

-- 删除数据:限制记录数为10
delete from my_copy where name='python102' limit 10;
delete from my_student where id=5;
-- 给my_student表增加主键
alter table my_student modify id int primary key auto_increment:

-- 清空表:重置自增长
truncate my_student;

-- select 选项
select * from my_copy;
select all * from my_copy;

-- 去重
select distinct * from my_copy;

-- 向学生表插入数据
insert into my_student
values(null,'bc20190001','张三','男'),
(null,'bc20190001','李四','男'),
(null,'bc20190001','王五','男'),
(null,'bc20190001','赵六','男'),
(null,'bc20190001','麻子','女');

-- 字段别名
select id,number as 学号,
name as 姓名,
sex 性别 from my_student;

-- 多表数据源
select * from my_student,my_class;

-- 子查询
select * from (select * from my_student) as s;

-- 增加age年龄和height身高字段
alter table my_student add age tinyint unsigned;
alter table my_student add height tinyint unsigned;

-- 增加字段值:rand取得一个0-1之间的随机数,floor向下取整
--rand()*20 => 0-20 + 20 => 20-40
update my_student set age=floor(rand()*20+20),
height=floor(rand()*20+170)

-- 找学生id为1,3,5的学生
select * from my_student where
id=1 || id=3 || id=5;-- 逻辑判断
select * from my_student where id
in(1,3,5);-- 落在集合中

-- 找身高在180-190之间的学生
select * from my_student where
height>=180 and height<=190;
select * from my_student where
height between 180 and 190;
select * from my_student where
height between 180 and 190;
select * from my_student where 1;
-- 所有条件都满足

-- 根据性别分组
select * from my_student group by sex;

-- 分组统计:身高高矮,平均年龄,总年龄
select sex,count(*),max(height),
main(height),avg(age),sum(age) from
my_student group by sex;

-- 修改id为4的记录,八年零设置为NULL
update my_student set age=null where id=4;
select sex,count(*),count(age),max(height),
min(height),avg(age),sum(age) from my_student
group by sex;

-- 修改id为1的记录,性别设置为女
update my_student set sex='女' where id=1;

select sex,count(*),count(age),max(height),
min(height),avg(age),sum(age) from my_student
group by sex desc;

alter table my_class drop primary key;
alter table my_class add id int primary key
auto_increment;
alter table my_student add c_id int;
update my_student set c_id=ceil(rand()*3);

-- 多字段分组:先班级,后男女
select c_id,sex,count(*),group_concat(name)
from my_student group by c_id,sex;-- 多字段排序


-- 求出所有班级人数大于等于2的学生人数
select c_id,count(*) from my_student group
by c_id having count(*)>=2;

select c_id,count(*) from my_student where count(*)>=2
group by c_id;

select c_id,count(*) as total from my_student group
by c_id having total>=2;

select c_id,count(*) as total from my_student where
total>=2 group by c_id;

-- having子句进行条件查询
select name as 名字,number as 学号 from my_student
having 名字 like '张%';

-- 排序
select * from my_student group by c_id;
select * from my_student order by c_id;

-- 多字段排序:先班级排序,后性别排序
select * from my_student order by c_id,sex desc;


-- 查询学生:前两个
select * from my_student limit 2;

-- 查询学生:前两个
select * from my_student limit 0,2;-- 记录数是从0开始编号
select * from my_student limit 2,2;-- 记录数是从0开始编号
select * from my_student limit 4,2;

-- 更改id为班级表的第一列
alter table my_class chang id id int first;


-- 交叉连接
-- my_student cross join my_class是数据源
select * from my_student cross join my_class;

-- 内连接
select * from my_student inner join my_class on
my_student.c_id=my_class.id;

select * from my_student inner join my_class on
my_student.c_id=my_class.id;
select * from my_student inner join my_class on
my_student.c_id=id; -- 两张表都有id字段

-- 字段和表别名
select s.*,c.name as c_name,c.room
from my_student as s inner join my_class as
c on s.c_id=c.id;

-- 把学生id为5的c_id设置NULL
update my_student set c_id=null where id=5;

-- where代替
select s.*,c.name as c_name,c.room from my_student as
s inner join my_class as c where s.c_id=c.id;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容

  • 知识点 字体综合设计font属性用于对字体样样式进行综合设置,其基本语法格式如下:选择器{font:font-st...
    睡到自然醒_52阅读 424评论 1 1
  • 时间真的是一个无法去衡量的东西,它在你不知不觉中慢慢流逝。小的时候,盼望着长大,长大以后却羡慕小时候的无忧无虑,单...
    xiao宝贝阅读 90评论 1 0
  • 秦淮凤鸣阅读 91评论 0 6
  • 定位:如果用三个标签介绍自己,是哪三个? 说起介绍自己,这些年来自己的身份一直在变来变去。在曾经的学生与同事面前...
    WLM_e375阅读 137评论 0 2
  • 对我们这个时代进行极其严肃的思考,免不了要与那种无家可归感相抗争。历史变迁的非人性的加速所带来的人类体验的不可靠感...
    孙武芮雯阅读 290评论 0 3