tnblog
首页
视频
资源
登录
什么时候才能领悟,取之越多失之越多
排名
4
文章
473
粉丝
3
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

mysql多表查询与子查询

4921人阅读 2022/5/3 23:07 总访问:1223124 评论:0 收藏:0 手机
分类: 数据库

代码如下:

  1. /* 笛卡尔积 */
  2. select * from students,class
  3. /* 内连接 */
  4. -- 隐式内连接
  5. select username,classname from students e,class c where e.classid = c.id
  6. -- 显示内连接
  7. select username,classname from students e inner join class c on e.classid = c.id
  8. /* 外连接 */
  9. -- 左外连接
  10. select username,classname from students e left outer join class c on e.classid = c.id
  11. -- 右外连接
  12. select username,classname from students e right outer join class c on e.classid = c.id
  13. /* 自连接 */
  14. select * from students
  15. -- 查询员工以及直属领导的名称
  16. select s.username,leader.username from students s join students leader on s.managerid = leader.id
  17. -- 查询员工以及直属领导的名称,没有领导的也需要查询出来,如果没有领导就显示老大
  18. select s.username,ifnull(leader.username,'老大') from students s left join students leader on s.managerid = leader.id
  19. /* 联合查询 */
  20. -- 把学生表年龄小于50的与学生分表中薪水小于8000的都查询出来
  21. -- UNION all 直接把查询结果进行合并
  22. select * from students where age>40
  23. UNION all
  24. select * from student_cq where salary <8000
  25. -- UNION 把查询结果合并之后去重
  26. select * from students where age>40
  27. UNION
  28. select * from student_cq where salary <8000

子查询:(后面把子查询拆分成另外一个,然后把这个文章转载成我自己的文章)

  1. /*
  2. 子查询分类:
  3. 标量子查询(子查询结果是单个值)
  4. 列子查询(子查询结果是一列)
  5. 行子查询(子查询结果是一行)
  6. 表子查询(子查询结果是一个表)
  7. 子查询位置:where , from ,select 都有可能
  8. */
  9. /* 标量子查询 */
  10. -- 查询1213班的所有学生信息
  11. select * from class where ClassName = '计网1213'
  12. select * from students where classid = 3
  13. select * from students where classid = (select id from class where ClassName = '计网1213')
  14. -- 查询紫嫣之后入职的员工
  15. /* 列子查询
  16. 可以使用in(在什么里边)
  17. all(一列的每个条件都需要满足)
  18. any(满足一个即可)
  19. */
  20. -- 查询计网1211班与1212班所有的员工信息
  21. -- 查询比计网1213班所有人工资都高的人(比任何一个都大这里可以使用all,或者max函数)
  22. -- 第一步:查询1213班所有的人工资
  23. select salary from students where classid = (select id from class where ClassName = '计网1213')
  24. -- 第二步:找出比这些工资都高的人,使用max函数
  25. select * from students where salary >
  26. (select max(salary) from students where classid = (select id from class where ClassName = '计网1213'))
  27. -- 方法2all关键字会挨着一个一个比较,要比所有的都高,其实就是比最高的高就行了
  28. select * from students where salary > all
  29. (select salary from students where classid = (select id from class where ClassName = '计网1213'))
  30. -- 查询比计网1211班任意一人工资高的人(不需要比所有人都高)(可以使用any
  31. select id from class where classname = '计网1211'
  32. select salary from students where classid = (select id from class where classname = '计网1211')
  33. -- 方法1 min函数
  34. select * from students where salary >
  35. (select min(salary) from students where classid = (select id from class where ClassName = '计网1211'))
  36. -- 方法2 any
  37. select * from students where salary > any
  38. (select salary from students where classid = (select id from class where ClassName = '计网1211'))
  39. /* 行子查询 */
  40. -- 查询与诸葛亮薪资与直属领导相同的员工
  41. select * from students where (salary,managerid) = (12500,1)
  42. select * from students where (salary,managerid) = (select salary,managerid from students where username ='诸葛亮')
  43. /*表子查询*/
  44. -- 查询与赵云和猪八戒职位和薪资都相同的员工(和上面一样使用in即可)
  45. -- 方法1:单个单个查询然后在合并结合
  46. select * from students where (job,salary) = (select job,salary from students where username = '赵云')
  47. union
  48. select * from students where (job,salary) = (select job,salary from students where username = '诸八戒')
  49. -- 方法2:直接in效率会快不少
  50. select * from students where (job,salary) in (select job,salary from students where username='赵云' or username='诸八戒')
  51. -- 查询入职员工1990后之后的员工信息与班级
  52. select u.username,c.ClassName from (select * from students where entrydate > '1990-1-1') u join class c
  53. on u.classid = c.id

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价