标签:用户 一个 height join 结果 each 历史记录 into int
create user ‘guest‘@‘ip地址‘ identified by ‘123‘ //ipconfig
grant 权限的具体使用 on.to 用户名@ip地址 identified by ‘‘密码‘‘
某些情况下,当进行一个查询时,需要的条件或数据要用另外一个 select 语句的结果
where型子查询即把内层sql语句查询的结果作为外层sql查询的条件.
子查询要包含在括号内。
建议将子查询放在比较条件的右侧。
单行操作符对应单行子查询,多行操作符对应多行子查询。
单行操作符 右边子查询必须返回的是单个值,单行比较运算符(=,>,>=,<,<=,<>)
多行操作符 右边子查询可以返回多行,但必须是单列,ALL, ANY,IN 其中,ALL和ANY运算符必须与单行比较运算符(=,>,>=,<,<=,<>)结合使用
IN:等于任何一个
ALL:和子查询返回的所有值比较。例如:sal>ALL(1,2,3)等价于sal>1 && sal>2 && sal>3,即大于所有。
ANY:和子查询返回的任意一个值比较。例如:sal>ANY(1,2,3)等价于sal>1 or sal>2 or sal>3,即大于任意一个就可以。
EXISTS:判断子查询是否有返回结果(不关心具体行数和内容),如果返回则为TRUE,否则为FALSE。
from型子查询即把内层sql语句查询的结果作为临时表供外层sql语句再次查询
表中需要的数据是否存在或有历史记录
CREATE TABLE newadmin LIKE admin;
CREATE TABLE newadmin AS ( SELECT * FROM admin ) ;
CREATE TABLE newadmin LIKE admin; INSERT INTO newadmin SELECT * FROM admin;
CREATE TABLE newadmin LIKE shop.admin; CREATE TABLE newshop.newadmin LIKE shop.admin;
CREATE TABLE newadmin AS ( SELECT id, username AS uname, password AS pass FROM admin WHERE id<10 ) ;
create table tt ( eid int primary key auto_increment ) as ( select employee_id as eid,first_name,last_name,email from employees );
在 INSERT 语句中加入子查询。 不必书写 VALUES 子句。 子查询中的值列表应与 INSERT 子句中的列名对应。 INSERT INTO emp2 SELECT * FROM employees WHERE department_id = 90; 或 INSERT INTO sales_reps(id, name, salary, commission_pct) SELECT employee_id, last_name, salary, commission_pct FROM employees WHERE job_id LIKE ‘%REP%‘;
老师表
课程表
学生表
成绩表
-- 1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; SELECT * FROM(SELECT sid,score FROM sc WHERE cid=001) AS a1, (SELECT sid,score FROM sc WHERE cid=002) AS a2 WHERE a1.sid=a2.sid AND a1.score>a2.score;
-- 2、查询平均成绩大于60 分的同学的学号和平均成绩 select sid,avg(score) FROM sc GROUP BY sid HAVING AVG(score)>60
-- 3、查询所有同学的学号、姓名、选课数、总成绩; SELECT student.sid,sname,COUNT(sc.cid),SUM(score) FROM student INNER JOIN sc WHERE student.sid=sc.sid GROUP BY sid;
-- 4、查询姓“刘”的老师的个数; SELECT COUNT(teacher.tName) FROM teacher WHERE tName LIKE "刘%";
--5、查询没学过“李老师”课的同学的学号、姓名; SELECT student.sid,student.sname FROM student WHERE student.sid NOT IN (SELECT dis student.sid FROM teacher,course,sc,student WHERE teacher.tid=course.tid AND course.cid=sc.cid AND sc.sid=student.sid AND teacher.tName="李老师" GROUP BY student.sid);
-- 6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名; SELECT t1.sid,t1.sname FROM ( SELECT student.sid,student.sname FROM student,sc WHERE student.sid=sc.sid AND sc.cid="001") AS t1, (SELECT student.sid,student.sname FROM student,sc WHERE student.sid=sc.sid AND sc.cid="002") AS t2 WHERE t1.sid=t2.sid; -- 7、查询学过“李老师”所教的所有课的同学的学号、姓名; SELECT student.sid,student.sname FROM teacher,course,sc,student WHERE teacher.tid=course.tid AND course.cid=sc.cid AND sc.sid=student.sid AND teacher.tName="李老师" GROUP BY sc.sid;
-- 8、查询课程编号“c002”的成绩比课程编号“c001”课程成绩低的所有同学的学号、姓名; SELECT a1.sid,a1.sname FROM (SELECT student.sid,student.sname,sc.score FROM course,sc,student WHERE course.cid=sc.cid AND sc.sid=student.sid AND course.cid=002) AS a1, (SELECT student.sid,student.sname,sc.score FROM course,sc,student WHERE course.cid=sc.cid AND sc.sid=student.sid AND course.cid=001) AS a2 WHERE a1.score<a2.score GROUP BY a1.sid;
-- 9、查询所有课程成绩小于60 分的同学的学号、姓名; SELECT student.sid,student.sname,sc.cid,sc.score FROM sc,student WHERE sc.sid=student.sid AND sc.score<60;
-- 10、查询没有学全所有课的同学的学号、姓名; SELECT student.sid,student.sname FROM student INNER JOIN sc ON student.sid=sc.sid GROUP BY student.sid HAVING COUNT(cid)<(SELECT COUNT(cid) FROM course);
标签:用户 一个 height join 结果 each 历史记录 into int
原文地址:https://www.cnblogs.com/wanghongwei1202/p/13860951.html