• 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. 在mac osX下安装openCV,used for python

    OpenCV是个开源的图像处理库,里面的内容多多. 想了解很多其它,请自行百度咯~ 篇blog是记录在mac下.安装openCV.然后使用python来引用openCV库. 环境是: Python 2 ...

  2. ExtJs 日期相加,Grid表格列可编辑

    1.日期相加: Ext.Date.add(new Date(), Ext.Date.DAY, 15) 2.Grid表格列可编辑: {    header : "实际已交货量",   ...

  3. Android+Jquery Mobile学习系列(9)-总结和代码分享

    经过一个多月的边学习边练手,学会了Android基于Web开发的毛皮,其实开发过程中用Android原生API不是很多,更多的是HTML/Javascript/Css. 个人觉得基于WebView的J ...

  4. bzoj 4198 [ Noi 2015 ] 荷马史诗 —— 哈夫曼编码(k叉哈夫曼树)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4198 第一次写哈夫曼树!看了很多博客. 哈夫曼树 & 哈夫曼编码:https://w ...

  5. Anaconda/kickstart

    http://fedoraproject.org/wiki/Anaconda/Kickstart/zh-cn

  6. C语言程序创建文件

    #include <stdio.h>#include <stdlib.h>int main() { FILE *fp;if((fp=fopen("g:\\a.txt& ...

  7. sql将一个表中的数据插入到另一个表中

    sql将一个表中的数据插入到另一个表中 列名不一定要相同,只要你在HH中列出要插入列的列表跟select   from   mm表中的选择的列的列表一一对应就可以了,当然两边的数据类型应该是兼容的.  ...

  8. The Moronic Cowmpouter(负进位制转换)

    http://poj.org/problem?id=3191 题意:将一个整型的十进制整数转化为-2进制整数. #include <stdio.h> #include <algori ...

  9. 6月7号shiro

    Retains all Cache objects maintained by this cache manager :保留此缓存管理器维护的所有缓存对象 Destroyable可毁灭的 retain ...

  10. [hihocoder][Offer收割]编程练习赛60

    hohahola #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #in ...