查询--单表查询

访问格式和顺序:

1)Select...

2)from...3) where...

4)group by.... 5)having....

6)oreder by...

2 3 4 5 1 6

 

SELECT

  select * from +表名 --查询全部

  select  查询的列名 as可省略 别名 from 表名  --select Sno,Sname as ‘姓名’

  select 中的子句可为表达式,字符串常量,函数,列别名等

  select Sno,Cno,Grade*1.5 成绩,'河北师大' 学校 from SC

SELECT DISTINCT

  select distinct 列名 --去重

  select distinct Sno from SC

WHERE

  select Sno,Sname from Student where Sno = 1 

  select Sno,Sname from Student where Sage>=20

BETWEEN...AND....

  select Sno,Sname from Student where Sage between 20 and 30

  select Sno,Sname from Student where Sage not between 20 and 30

IN

  select Sno,Sname from Student where Sdept in('xx系','xx系')

等价于select Sno,Sname from Student where Sdept = 'xx系' or Sdept = 'xx系'

综合范例:

select Sname,Sdepe,Sage
from Student
where Ssex = '' or (Ssex = '' and Sage not in(20,23))--and优先级高于or

select distinct Sno
from SC
where Cno  in(1,2) and Grade >=90

LIKE 模糊查询

 

用ESCAPE将通配符进行转义 例如想要% _    escape '换码字符' 只转换紧随其后的第一个

where Sname like 'ab_c%' escape '\' --把字符串中_转换

 

 

 

 

 

 

 

 

 

  

  

posted @ 2022-04-06 09:39  山上的树  阅读(62)  评论(0)    收藏  举报