常用SQL语句

tb_course 数据举例

tb_aspect 数据举例

tb_category 数据举例

1、排序

select * from tb_course ORDER BY studyNum

2、隐式连接使用别名

select * from tb_aspect a , tb_category WHERE a.id=c.aspectId

3、显示连接使用别名

select * from tb_aspect a JOIN tb_category c ON a.id=c.aspectId

4、左外链接

select * from tb_course c LEFT JOIN tb_aspect a on c.aspectId=a.id

5、右外连接

select * from tb_course c RIGHT JOIN tb_aspect a on c.aspectId=a.id

6、最大值函数的使用

select MAX(studyNum) from tb_course

7、子查询(出现在where后是作为条件使用)

select * from tb_course where studyNum>(SELECT AVG(studyNum) FROM tb_course) 

8、子查询(出现在from后是作为表使用)

select id,title from (SELECT * FROM tb_course WHERE studyNum>=1000)

9、exists 关键字(当exists后的条件判断成功时,整条查询语句才能执行成功)

select * from tb_course where EXISTS (select * from tb_course where studyNum>1000)

10、单行单列举例

 SELECT * from tb_course where studyNum>=(SELECT avg(studyNum) from tb_course)

11、多行多列举例

SELECT * from tb_course where studyNum >=ALL (SELECT avg(studyNum) from tb_course)

12、单行多列举例

SELECT title,cover,studyNum from tb_course where id=1

13、mysql数据库不支持全连接,但是可以使用union 加上左右连接的方式实现类似功能

select * from tb_course c left join tb_aspect a on c.aspectId=a.id 

UNION

select * from tb_course c right join tb_aspect a on c.aspectId=a.id

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,248评论 0 7
  • ##一、数据库操作: ** 1、查看数据库: ** >SHOW DATABASES; ** 2、创建数据库: **...
    smallBear阅读 302评论 0 1
  • 50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna...
    最美的太阳WW阅读 3,231评论 0 23
  • 从知乎过来这边看看,先上点小基础,也给自己回顾以下sql语句的一些用法,直接上干货。 sql不分大小写! 一、数据...
    yoohooo阅读 279评论 0 0
  • 1.React-native Cannot find module 'invariant' 当你执行命令 reac...
    伟炜龙阅读 1,018评论 0 0