• mysql 修改一列自增长

set @rownum=0;

update a

SET id = (

select @rownum := @rownum +1 as nid)

WHERE id < 10;

l  mysql 查询重复数据及其条数

select login_name,count(*) as count from sys_user group by login_name having count>1;

  • mysql 查询一个表中没有存在在另一个表的数据

SELECT * FROM A  WHERE  id  NOT  IN  ( SELECT id FROM B);或者SELECT * FROM A  WHERE      NOT  EXISTS  (          SELECT 1          FROM B          WHERE B.id = A.id );或者SELECT    A.*  FROM    A  LEFT JOIN B     ON (A.id = B.id) WHERE   b.id  IS  NULL

1.添加PRIMARY KEY(主键索引)

mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

2.添加UNIQUE(唯一索引)

mysql>ALTER TABLE `table_name` ADD UNIQUE (

`column`

)

3.添加INDEX(普通索引)

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )

4.添加FULLTEXT(全文索引)

mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)

5.添加多列索引

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

  • 选择查询样例

<choose>

<when test="1==type">

<if test="0==chilId">

select id  resourceId,cmdb_app_id cmdbId,cmdb_app_name cmdbApplicationName,third_id chilId,type,

third_name otherApplicationName, create_time createTime from third_cmdb_mapping where type = #{type}

<if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

and cmdb_app_name   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

</if>

<if test=" null!= otherApplicationName and '' !=otherApplicationName">

and third_name   like CONCAT('%',trim('${otherApplicationName}'),'%')

</if>

</if>

l  <if test="1==chilId">

SELECT A.resource_id resourceId,A.pro_value cmdbApplicationName FROM  cmdb_resource A

WHERE A.pro_key = 'CINAME' and A.resource_type = 'APPLICATION' and A.resource_id  NOT  IN  ( SELECT B.cmdb_app_id FROM third_cmdb_mapping  B where type =#{type})

<if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

and  pro_value   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

</if>

<if test=" null!= otherApplicationName and '' !=otherApplicationName">

and 1=2

</if>

</if>

</when>

</choose>

  • 查询时给某列赋值

select u.id,u.name,

(case u.sex

when 1 then '男'

when 2 then '女'

else '空的'

end     )性别

from users u;

  • case 第二种用法 判断  条件在when开始写
  • select ENAME,job,(case
    when sal<500 then '低级员工'
    when 500<sal and sal<1000 then '中级员工'
    when sal >1000 then '高级员工'
    else ''       else 可以不写
    end) jibei
    from emp;

  • mysql 模糊查询 去除空格

<if test=" null!= cmdbApplicationName and '' !=cmdbApplicationName">

and cmdb_app_name   like CONCAT('%',trim('${cmdbApplicationName}'),'%')

</if>

练习sql

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

select s.sname name,s.ssex sex,s.class class from students s;

2、 查询教师所有的单位即不重复的Depart列。

select DISTINCT (t.depart) depart from teachers t;

3、 查询Student表的所有记录。

select * from students;

4、 查询Score表中成绩在60到80之间的所有记录。

select * from scores where degree BETWEEN 60 and 80;

5、 查询Score表中成绩为85,86或88的记录。

select * from scores where degree in ('85','86','88');

6、 查询Student表中“95031”班或性别为“女”的同学记录。

select * from students s where s.class = '95031' or s.ssex ='女'

7、 以Class降序查询Student表的所有记录。

select * from students ORDER BY class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。

select * from scores s ORDER BY s.cno asc,s.degree desc;

9、 查询“95031”班的学生人数。

select COUNT(1) from students s where s.class = '95031'

10、查询Score表中的最高分的学生学号和课程号。

select s.cno cno,s.sno sno from scores s ORDER BY s.degree desc LIMIT 1;

11、查询‘3-105’号课程的平均分。

select s.cno, Avg(s.degree) avg

from scores s ORDER BY s.cno and s.cno = '3-105';

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select s.sno,AVG(degree) from scores s WHERE s.cno like '3%' group by s.cno HAVING COUNT(s.sno) >= 5;

13、查询最低分大于70,最高分小于90的Sno列。

SELECT Sno

FROM Scores

GROUP BY Sno

HAVING MAX(Degree)<90 AND MIN(Degree)>70;

14、查询所有学生的Sname、Cno和Degree列。

select s.sname,c.cno,c.degree from students s inner join scores c on s.sno = c.sno ORDER BY s.sname

15、查询所有学生的Sno、Cname和Degree列。

select a.sno,b.degree,c.cname from test.students a,test.scores b,test.courses c where a.sno = b.sno and c.cno = b.cno;

16、查询所有学生的Sname、Cname和Degree列。

select a.sname,b.degree,c.cname from test.students a,test.scores b,test.courses c where a.sno = b.sno and c.cno = b.cno;

17、查询“95033”班所选课程的平均分。

SELECT Cname,AVG(Degree)

FROM Students INNER JOIN Scores

ON(Students.Sno=Scores.Sno) INNER JOIN Courses

ON(Scores.Cno=Courses.Cno)

WHERE Class='95033'

GROUP BY Courses.Cno

ORDER BY Cname;

18、假设使用如下命令建立了一个grade表:

create table grade(low   number(3,0),upp   number(3),rank   char(1));

insert into grade values(90,100,’A’);

insert into grade values(80,89,’B’);

insert into grade values(70,79,’C’);

insert into grade values(60,69,’D’);

insert into grade values(0,59,’E’);

commit;

现查询所有同学的Sno、Cno和rank列。

SELECT Sno,Cno,rank

FROM Scores INNER JOIN grade

ON(Scores.Degree>=grade.low AND Scores.Degree<=grade.upp)

ORDER BY Sno;

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

--标准答案

SELECT s1.Sno,s1.Degree

FROM Scores AS s1 INNER JOIN Scores AS s2

ON(s1.Cno=s2.Cno AND s1.Degree>s2.Degree)

WHERE s1.Cno='3-105' AND s2.Sno='109'

ORDER BY s1.Sno;

--自己写的垃圾sql

SELECT * FROM students  inner join scores on students.sno = scores.sno  inner join courses on courses.cno = scores.cno where courses.cno = '3-105' and  scores.degree > ( select degree from students inner join scores on students.sno =scores.sno inner join courses on courses.cno = scores.cno where students.sno='109' and courses.cno = '3-105') order by students.sno;

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

SELECT *

FROM Scores

GROUP BY Sno

HAVING COUNT(cno)>1 AND Degree!=MAX(Degree);

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

--标准答案

SELECT s1.Sno,s1.Degree

FROM Scores AS s1 INNER JOIN Scores AS s2

ON(s1.Cno=s2.Cno AND s1.Degree>s2.Degree)

WHERE s1.Cno='3-105' AND s2.Sno='109'

ORDER BY s1.Sno;

--垃圾sql

select * from  scores a where a.degree>(

select b.degree from scores  b where b.sno ='109' and b.cno = '3-105'

) order by a.sno

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select s1.sno,s1.sname,s1.sbirthday from students s1  inner join students s2

on (YEAR(s1.sbirthday) = YEAR(s2.sbirthday)) where s1.sno='108'

23、查询“张旭“教师任课的学生成绩。

SELECT Sno,Degree

FROM Scores INNER JOIN Courses

ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers

ON(Courses.Tno=Teachers.Tno)

WHERE Teachers.Tname='张旭';

24、查询选修某课程的同学人数多于5人的教师姓名。

--自己写的还算凑合

select t.tname from courses c

inner join teachers t

on c.tno = t.tno

inner join scores e

on e.cno = c.cno

inner join students s

on s.sno = e.sno

group by c.cno

HAVING COUNT(s.sno)>=5;

--标准答案

SELECT DISTINCT Tname

FROM Scores INNER JOIN Courses

ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers

ON(Courses.Tno=Teachers.Tno)

WHERE Courses.Cno IN(SELECT Cno FROM Scores GROUP BY(Cno) HAVING COUNT(Sno)>5);

25、查询95033班和95031班全体学生的记录。

SELECT *

FROM Students

WHERE Class IN ('95033','95031')

ORDER BY Class;

26、查询存在有85分以上成绩的课程Cno.

SELECT DISTINCT Cno

FROM Scores

WHERE Degree>85;

27、查询出“计算机系“教师所教课程的成绩表。

select c.degree from courses a

inner join teachers b

on a.tno = b.tno

inner join scores c

on c.cno = a.cno

where b.depart = '计算机系'

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

--标准答案

SELECT Tname,Prof

FROM Teachers

WHERE Depart='计算机系' AND Prof NOT IN(

SELECT DISTINCT Prof

FROM Teachers

WHERE Depart='电子工程系');

--垃圾答案

select DISTINCT(prof) from teachers

where depart= '计算机系' or depart= '电子工程系'

GROUP BY prof

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

SELECT Cno,Sno,Degree

FROM Scores

WHERE Cno='3-105' AND Degree > ANY(

SELECT Degree

FROM Scores

WHERE Cno='3-245')

ORDER BY Degree DESC;

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

SELECT Cno,Sno,Degree

FROM Scores

WHERE Cno='3-105' AND Degree > ALL(

SELECT Degree

FROM Scores

WHERE Cno='3-245')

ORDER BY Degree DESC;

31、查询所有教师和同学的name、sex和birthday.

SELECT Sname,Ssex,Sbirthday         FROM Students         UNION         SELECT Tname,Tsex,Tbirthday         FROM Teachers;

32、查询所有“女”教师和“女”同学的name、sex和birthday.

SELECT Sname,Ssex,Sbirthday        FROM Students  WHERE Ssex='女'  UNION  SELECT Tname,Tsex,Tbirthday  FROM Teachers  WHERE Tsex='女';

33、查询成绩比该课程平均成绩低的同学的成绩表。

SELECT s1.*

FROM Scores AS s1 INNER JOIN (

SELECT Cno,AVG(Degree) AS aDegree

FROM Scores

GROUP BY Cno) s2

ON(s1.Cno=s2.Cno AND s1.Degree<s2.aDegree);

34、查询所有任课教师的Tname和Depart.

SELECT Tname,Depart  FROM Teachers  WHERE Tno IN(      SELECT Tno      FROM Courses  );

35  查询所有未讲课的教师的Tname和Depart.

SELECT Tname,Depart  FROM Teachers  WHERE Tno NOT IN(      SELECT Tno     FROM Courses  );

36、查询至少有2名男生的班号。

-- 自己代码 都可以

select * from students GROUP BY class HAVING COUNT(ssex) > 2

--提供sql

SELECT Class,COUNT(1) AS boyCount

FROM Students

WHERE Ssex='男'

GROUP BY Class

HAVING boyCount>=2;

37、查询Student表中不姓“王”的同学记录。

SELECT * FROM Students WHERE Sname NOT LIKE '王%';

38、查询Student表中每个学生的姓名和年龄。

select YEAR(NOW()) -YEAR(sbirthday)  from students

39、查询Student表中最大和最小的Sbirthday日期值。

SELECT MIN(Sbirthday),MAX(Sbirthday) FROM Students;

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

SELECT *  FROM Students  ORDER BY Class DESC,Sbirthday ASC;

41、查询“男”教师及其所上的课程。

select * from teachers inner join courses on teachers.tno = courses.tno where teachers.tsex = '男'

42、查询最高分同学的Sno、Cno和Degree列。

SELECT *

FROM Scores

GROUP BY Cno

HAVING Degree=Max(Degree);

43、查询和“李军”同性别的所有同学的Sname.

SELECT s1.Sname

FROM Students AS s1 INNER JOIN Students AS s2

ON(s1.Ssex=s2.Ssex)

WHERE s2.Sname='李军';

44、查询和“李军”同性别并同班的同学Sname.

SELECT s1.Sname

FROM Students AS s1 INNER JOIN Students AS s2

ON(s1.Ssex=s2.Ssex and s1.class=s2.class)

WHERE s2.Sname='李军';

45、查询所有选修“计算机导论”课程的“男”同学的成绩表

--自己写的ok代码

select * from courses a inner join scores b

on a .cno = b.cno inner join students  c

on b.sno= c.sno

WHERE a.cname = '计算机导论' and c.ssex = '男';

--提供答案

SELECT *

FROM Scores

WHERE Sno IN (

SELECT Sno

FROM Students

WHERE Ssex='男') AND

Cno IN (

SELECT Cno

FROM Courses

WHERE Cname='计算机导论');

sql积累的更多相关文章

  1. 项目积累——SQL积累

    select sum(njts)-sum(ysyts) from njsyqk where ygdh='888882' and ((yxbz is null) or (yxbz='1')) selec ...

  2. 【原】MySQL实用SQL积累

    [文档简述] 本文档用来记录一些常用的SQL语句,以达到快速查询的目的. [常用SQL] 1.mysql数据库中获取某个表的所有字段名 select COLUMN_NAME from informat ...

  3. oracle常见sql积累

    select lower('HELLO') from dual;select lpad(100, 5, '*') from dual;select sysdate + 1 / 24 from dual ...

  4. SQL常见问题积累

    SQL积累--仅适用于SQL Server 1.sql中,字符串保存序号,按照数字顺序进行排序 ))),) asc --householdNo 为要排序字段 2.控制小数位数 ,),,)))+'%' ...

  5. 积累一下SQL

    开篇先自我检讨一下,写了博客几年以来首次试过连续两个月没出过博文,有客观也有主观原因,但是最近这年里博文数量也越来越少,博文的质量也每况日下.希望自己一直能坚持下来,多写写博文,这月尽量多写几篇来弥补 ...

  6. [强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!)

    原文:[强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!) [强烈推荐]ORACLE PL/SQL编程详解之七: 程序包的创建与应用(聪明在于学习,天 ...

  7. 一学期积累下来的SQL语句写法的学习

    整合了一下上学期学习的积累,希望可以帮到初学者! 可能以后会有用吧! A 基本语句的运用 操作基于emp表1.按工资从高到低排列SQL> select rownum as 次序,ename,sa ...

  8. sql查询学习和实践点滴积累

    https://blog.rjmetrics.com/2008/10/28/correlated-subqueries-in-mysql/ http://www.mysqltutorial.org/m ...

  9. sql 的积累

    sql的积累 By:山高似水深 原创 转载注明出处 .REVERSE() 反转 例如: Hive 可用 2016年12月3日11:31:59 2.instr(str,'.')位置 结果:得出在str中 ...

随机推荐

  1. javac compiling error ( mising package)

    javac 编译java源文件时,提示 package does not exist 的错误 Test.java import java.security.MessageDigest; import ...

  2. c语言文件包含

    文件包含是指一个C语言源程序中将另一个C语言源程序包含进来,通过include预处理指令实现. 一般形式: #include”被包含文件名” 或#include<被包含文件名> 2.  作 ...

  3. 【SSH之旅】一步步学习Hibernate框架(一):关于持久化

    在不引用不论什么框架下,我们会通过平庸的代码不停的对数据库进行操作,产生了非常多冗余的可是又有规律的底层代码,这样频繁的操作数据库和大量的底层代码的反复书写极大的浪费了程序人员的书写.就在这样一种情况 ...

  4. java读取中文分词工具(一)

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  5. springmvc 处理lsit类型的请求參数映射成实体属性

    <table align="center" cellspacing="10"> <tr> <td> 母码数目:<inp ...

  6. samba笔记

    ############ 1.安装网络yum ############ 2.安装createrepo [root@localhost ~]# yum install createrepo-0.9.8- ...

  7. How to use shared model by git in sql source control of red gate

    1.clone the git repository for datbase 2.open sql source control window and select the target databa ...

  8. 【HDU 6162】 Ch’s gift

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6162 [算法] 离线树剖 我们知道,u到v路径上权值为[A,B]的数的和 = u到v路径上权值小于 ...

  9. go并发编程 WaitGroup, Mutex

    1.背景 记录一下,方便后续写代码直接使用. 需要注意几点: chan 默认支持多协程工作,不需要加锁. 其他变量操作需要使用锁保护(map多协程并发写会panic, 并且无法捕获). 启动gorou ...

  10. CAS配置(1)SSL证书配置

    一.配置源码 源码配置稍后提供 二.系统环境安装 安装JDK配置,版本>=1.7 环境变量配置(参考): JAVA_HOME=C:\Program Files x86)\Java\jdk1.7. ...