美文网首页
【MySql】mysql常用语句

【MySql】mysql常用语句

作者: Y了个J | 来源:发表于2018-05-22 13:44 被阅读9次
拼接字符串
select concat('我叫', name, '我的性别:', sex) from user limit 1;
截取字符串
select substring("abcdefg", 1 , 3);
去除空格
select rtrim(name) from user where id =1;
select ltrim(name) from user where id =1;
select trim(name) from user where id =1;

mysql默认会把字符串和日期进行转换
DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d');

在emp表中,列出每年入职的人数和
select count(1), year(hiredate), max(sal) from emp group by year(hiredate);

创建视图,给别人用时可以隐藏emp里其他重要信息,只对外部分字段。可以简化查询语句
create view v_emp as select empno,ename,job from emp;
select * from v_emp;
insert into v_emp values(8888,'zs','clack');

查询工资比程序员低的人
select name, sal from emp where sal < all (select sal from emp where job = '程序员');
创建表成绩详情表: 
CREATE TABLE score ( 
id int(10) NOT NULL AUTO_INCREMENT, 
subject_id int(10) DEFAULT NULL, 
student_id int(10) DEFAULT NULL, 
score float DEFAULT NULL, 
emp_max_count int(2) check(emp_max_count>0 and emp_max_count<20),
PRIMARY KEY (id) 
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8; 
屏幕快照 2018-05-22 下午1.21.42.png
创建学生表: 
CREATE TABLE student ( 
id int(10) NOT NULL AUTO_INCREMENT, 
name varchar(10) DEFAULT NULL, 
PRIMARY KEY (id) 
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; 
屏幕快照 2018-05-22 下午1.22.48.png
创建科目表: 
CREATE TABLE subject ( 
id int(10) NOT NULL AUTO_INCREMENT, 
name varchar(10) DEFAULT NULL, 
PRIMARY KEY (id) 
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 
屏幕快照 2018-05-22 下午1.23.39.png
select a.id,a.subject_id,a.student_id,a.score from score as a left join score as b on
a.subject_id=b.subject_id and a.score>=b.score
group by a.subject_id,a.student_id,a.score
having count(a.subject_id)>=4
order by a.subject_id,a.score desc;
SELECT c.* , tbt.num '班级人数'  FROM class c 
LEFT JOIN (SELECT count(sid) num , cname  FROM student GROUP BY cname HAVING count(sid) > 30 ) tbt
WHERE c.cname = tbt.cname ;
数据库有2张表,一张学生表,一张成绩表,查询某学生的成绩,sql语言怎么写?

根据2张表关联的字段查询,比如学生表有学生ID字段,成绩表如果也有这个ID,则
select * from student inner join score on student.ID=score.学生ID
1.学生表
CREATE TABLE `Student` (  
  `sid` int(32) NOT NULL AUTO_INCREMENT,  
  `sname` varchar(32) DEFAULT NULL,  
  `ssox` varchar(32) DEFAULT NULL,  
  `sbirth` date DEFAULT NULL,  
  PRIMARY KEY (`sid`)  
)  

2.选课表
CREATE TABLE `selectCourse` (  
  `sid` int(32) DEFAULT NULL,  
  `cid` int(32) DEFAULT NULL  
)  

3.课程表
CREATE TABLE `Course` (  
  `cid` int(32) NOT NULL AUTO_INCREMENT,  
  `cname` varchar(32) DEFAULT NULL,  
  `cteacher` varchar(32) DEFAULT NULL,  
  PRIMARY KEY (`cid`)  
)  

INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('1', '神奇的我', '男', '1999-1-1');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('2', '神奇的他', '女', '1999-9-9');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('3', '老王', '男', '2000-12-12');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('4', '老张', '男', '2000-11-11');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('5', '小美', '女', '2001-1-1');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('6', '小明', '男', '2010-3-2');  
INSERT INTO `Student` (`sid`, `sname`, `ssox`, `sbirth`) VALUES ('7', '小张', '男', '2012-12-12');  
  
INSERT INTO `Course` (`cid`, `cname`, `cteacher`) VALUES ('1', '数学', '隔壁老王');  
INSERT INTO `Course` (`cid`, `cname`, `cteacher`) VALUES ('2', '英语', '隔壁老张');  
INSERT INTO `Course` (`cid`, `cname`, `cteacher`) VALUES ('3', '体育', '隔壁老王');  
INSERT INTO `Course` (`cid`, `cname`, `cteacher`) VALUES ('4', '语文', '隔壁老李');  
  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('1','1');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('1','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('1','3');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('2','1');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('2','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('2','3');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('3','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('4','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('5','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('6','2');  
INSERT INTO `selectCourse` (`sid`, `cid`) VALUES ('7','2');  

例如查询学生学号为1学生的有 哪些课程 分别什么老师
select cname,cteacher from Course inner join selectCourse on selectCourse.cid=Course.cid and sid=1;  

例如查询老师叫 隔壁老王的老师 体育课程的学生
意思是  选择学生姓名  来自 学生表  条件 
(来自教师表 和 选课表 中 当选课表课程id=3 并且 教师表课程id=3 并且老师为 隔壁老王)
select sname from Student where sid in
(select sid  from  Course inner join selectCourse on selectCourse.cid=3 and Course.cid=3  and cteacher='隔壁老王');  

相关文章

  • BigData-MySQL总结大全(一)苏暖人

    BigData之MySQL总结大全 MYSQL常用的基本语句 MYSQL常用的基本语句 例:SELECT TOP ...

  • MySQL常用语句

    MySQL常用语句 tags: MySQL 常用语句 语法 随便写的标签 建表 insert 语句 msyql 把...

  • mysql(二)——用户管理、数据备份、常用sql语句

    13.4 mysql用户管理13.5 常用sql语句13.6 mysql数据库备份恢复 13.4 mysql用户管...

  • mysql

    常用语句 更新系统 下载PHP,MySQL,nginx mysql-server 数据库服务 mysql-clie...

  • MySQL常用操作(二)

    摘要:13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢...

  • MySQL常用语句

    本篇主要是归纳一下最常用、入门的 MySQL 语句。以安装完 MySQL 登录为起点,简单总结一下常用的几条语句。...

  • Mysql 语句

    Mysql 语句 Mysql 语句以考察各种常用连接为重点 几种常见连接 内连接(INNER JOIN):两个表都...

  • mysql 常用监控指标

    mysql 常用监控指标 TPS/QPS mysql.tps (Per Second) 平均每秒SQL语句执行次数...

  • MYSQL | 常用mysql语句

    1、创建数据库 2.删除数据库 3.选择数据库 4.创建表 注意: 5.删除数据表: 6.向数据表插入数据 插入所...

  • 一篇文章,总结MySQL基本操作

    MySQL的基本操作可以包括两个方面:MySQL常用语句如高频率使用的增删改查(CRUD)语句和MySQL高级功能...

网友评论

      本文标题:【MySql】mysql常用语句

      本文链接:https://www.haomeiwen.com/subject/mbphjftx.html