当前位置:   article > 正文

Mysql第二章(DML和DQL)_year(now())-year(borndate) as age

year(now())-year(borndate) as age

本章目标

1、掌握使用sql语句修改、删除表

2、掌握添加/删除约束的sql语句

3、掌握简单子查询的用法

4、掌握IN子查询的用法


存储引擎的类型

MyISAM、InnoDB、Memory、CSV等九种


设置表的存储引擎

create table myschool(

    dis int(3)

)engine=存储引擎    #myisam/innodb


插入单条数据

insert into 表名(字段名) values(值列表)



将查询结果插入新表

create table 表名(

    select 列名 

    from 表名

    where 条件 

    order by 列名     #desc 

)    



数据更新

update 表明 set 列名=()

where 


删除数据记录

1,delect from 表名

where

  1. DELETE FROM dropstudent
  2. WHERE studentName='郭靖'

2,truncate table 表名







常用函数 聚合函数

AVG()          平均

COUNT()     行数

MAX()         最大

MIN()          最小

SUM()         求和

RAND()       返回0-1直接的某个数


练习:把成绩都降低10%后加5分,再查询及格成绩,并从高到低排序



Mysql查询语句中使用LIMIT字句限制结果集

select 字段名/列

from 表名

where 条件

group by 分组字段名

order by  排序字段名  #desc

limit (位置偏移量,行数)

  1. SELECT `studentNo`,`studentName`,`phone`,`address`,`bornDate`
  2. FROM student
  3. WHERE gradeID=1
  4. ORDER BY studentNo
  5. LIMIT 2,3 #从第几条开始,显示几条 不写默认0 [位置偏移量,行数]



  1. UPDATE student SET loginPwd='000',email='stu20000@163.com'
  2. WHERE studentNo=20000
  3. UPDATE `subject` SET classHour=classHour-10
  4. WHERE classHour>200 AND gradeID=1
  5. CREATE TABLE student_grade1(
  6. SELECT `studentName`,`sex`,`bornDate`,`phone`
  7. FROM student
  8. WHERE gradeID=1
  9. )

1、

  1. SELECT studentNo,studentResult
  2. FROM result
  3. WHERE examDate='2016-02-17'
  4. ORDER BY studentResult DESC
  5. LIMIT 5

2、

  1. SELECT studentName,YEAR(NOW())-YEAR(bornDate) AS 年龄,bornDate,phone
  2. FROM student
  3. WHERE sex='女'
  4. ORDER BY 年龄 DESC
  5. LIMIT 1,6

3、

  1. SELECT YEAR(bornDate) AS 年份,COUNT(studentName) AS 人数
  2. FROM student
  3. GROUP BY 年份
  4. HAVING 人数>2
4、
  1. SELECT studentNo,MAX(studentResult)AS 最高分,MIN(studentResult) AS 最低分,
  2. AVG(studentResult) AS 平均分
  3. FROM result
  4. WHERE examDate='2016-02-17'

  1. SELECT `studentNo`,`loginPwd`,`studentName`,
  2. `sex`,`gradeID`,`phone`,`address`,`bornDate`,
  3. `email`,`identityCard`
  4. FROM student
  5. WHERE bornDate>
  6. (SELECT bornDate
  7. FROM student
  8. WHERE studentName='李斯文')


  1. SELECT MAX(studentResult) AS 最高分, MIN(studentResult) AS 最低分
  2. FROM result
  3. WHERE subjectNo=(SELECT subjectNo FROM `subject`WHERE subjectName='Logic Java')
  4. AND examDate=(SELECT MAX(examDate)
  5. FROM result
  6. WHERE subjectNo=(SELECT subjectNo FROM `subject`WHERE subjectName='Logic Java'))


  1. SELECT studentName
  2. FROM student
  3. WHERE studentNo IN(SELECT studentNo #因为返回结果不是一个 要用in子查询
  4. FROM result
  5. WHERE subjectNo =(SELECT subjectNo FROM `subject`WHERE subjectName='Logic Java'))


  1. SELECT studentNo,studentName
  2. FROM student
  3. WHERE studentNo IN(
  4. SELECT studentNo
  5. FROM result
  6. WHERE examDate IN(
  7. SELECT MAX(examDate)
  8. FROM result
  9. WHERE subjectNo=(
  10. SELECT subjectNo
  11. FROM `subject`
  12. WHERE subjectName='Logic Java'))
  13. AND subjectNo=(
  14. SELECT subjectNo
  15. FROM `subject`
  16. WHERE subjectName='Logic Java'))



NOT IN
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/701207
推荐阅读
相关标签
  

闽ICP备14008679号