sql做题第三天

简介: sql语法必做练习

第七例:查找学校是北大得学生信息

droptable if exists user_profile;

CREATETABLE `user_profile` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`gender` varchar(14) NOTNULL,

`age` int ,

`university` varchar(32) NOTNULL,

`province` varchar(32)  NOTNULL);

INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');

INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');

INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');

INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');

INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

  • 法一:先找出id与学校名称,再使用条件符进行分类:select device_id,university from user_profile where university='北京大学';
  • 法二:使用like模糊查询:select device_id,university from user_profile where university like '北京大学';

第八例:查找除复旦大学得用户信息

droptable if exists user_profile;

CREATETABLE `user_profile` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`gender` varchar(14) NOTNULL,

`age` int ,

`university` varchar(32) NOTNULL,

`province` varchar(32)  NOTNULL);

INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');

INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');

INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');

INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');

INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

  • 法一:使用!=得条件进行检索:select device_id,gender,age,university from user_profile where university!='复旦大学';
  • 法二:使用not in('A','B','C')后面需要加():select device_id,gender,age,university from user_profile where university not in ('复旦大学');
  • 法三:使用not like模糊查询:select device_id,gender,age,university from user_profile where university not like '复旦大学';
  • 法四:使用<>条件作用与使用!=用法一样:select device_id,gender,age,university from user_profile where university <> '复旦大学';

第九例:Where in和Not in

droptable if exists user_profile;

CREATETABLE `user_profile` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`gender` varchar(14) NOTNULL,

`age` int ,

`university` varchar(32) NOTNULL,

`province` varchar(32)  NOTNULL,

`gpa` float);

INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing',3.4);

INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai',4.0);

INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing',3.2);

INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang',3.6);

INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong',3.8);

  • 法一:使用where in()进行分类in范围内得会被检索出来:select device_id,gender,age,university,gpa from user_profile where university in ('北京大学','复旦大学','山东大学');
相关文章
|
SQL 索引
sql做题第十六天(删除记录篇)
• 扩展:在 delete 后加 limit 是个好习惯。原因如下: • 1,delete from 是全表查找的,如果加上limit 时,删除指定的条数后,就会return了。效率提高不少。 • 2,降低写错 SQL 的代价,即使删错了,例如limit 100,也就删除了100条数据,也能通过binlog找回数据 • 3,避免长事务,delete执行时,涉及的行是会加锁,如果删除的数据量大,那业务功能都要不能用了 • 4,加锁都是基于索引的,如果查询字段没有加索引,那会扫描到主键索引上,那么就算查询出来的只有一条记录,也会锁表 • 5,delete数据量大时,容易占用cpu,导致越删除越慢
|
SQL 关系型数据库 MySQL
sql做题第十五天(更新记录篇)
第三十七例:更新记录(2) • 题目地址:更新记录(二)牛客题霸牛客网 (nowcoder.com) • 初始化数据:
|
SQL 算法 索引
sql做题第十四天(插入记录)
• 题目描述:牛客后台会记录每个用户的试卷作答记录到exam_record表,现在有两个用户的作答记录详情如下: • 用户1001在2021年9月1日晚上10点11分12秒开始作答试卷9001,并在50分钟后提交,得了90分; • 用户1002在2021年9月4日上午7点1分2秒开始作答试卷9002,并在10分钟后退出了平台。 • 试卷作答记录表exam_record中,表已建好,其结构如下,请用一条语句将这两条记录插入表中。
|
SQL 移动开发
|
SQL Serverless
|
SQL 数据挖掘
sql做题第九天
sql语法必做