创建学生表,内有字段 姓名,年龄,性别,年级  ,(这里我只创建到三年级)

73371ef87c94a7ccedcfd7de3e95a5717fd.jpg

备注 :性别 男/女,,年级 :学前班/一到六年级,,姓名年龄不限
要求: 查询一年级的男生和二到五年级的所有学生

对于以上的查询语句该编写SQL

select * from student where grade in ('一年级','二年级','三年级') and (grade!='一年级' and sex!='女')

select * from student where 1=1 and  (grade!='一年级' and grade!='学前班'  and sex!='女')

select * from student where grade in ('二年级','三年级') or (grade='一年级' and sex='男')

这里我写出三个SQL,哪一个可以正确执行出结果呢?这里操作一下,返回结果如下:

b0aebc2d7b3142dd280ebb667263b28f750.jpg

6dfff671c80983065b24ebf806f9ae93e6e.jpg

 

3f86f1942f565003852ca19880f66b36e75.jpg

如图三正是我们想要的结果,第三条SQL是适合我们要求的SQL

 

转载于:https://my.oschina.net/u/3387320/blog/2977825

Logo

更多推荐