Linux - C数据库编程(预习内容四)

来源:互联网 发布:最好的seo课程 编辑:程序博客网 时间:2024/05/10 00:35

单表查询:

    查询是对数据库的最常用操作。查询的结果可以理解为得到了一个新表,只是这个表并没有保存在数据库里,而是以某种方式显示给查询者。

  用select语句可以对数据库进行查询

select ID, Name from student where Sex = 'F';

 查询结果中出现表的所有字段(*)

select * from score where  score < 60;

  查询结果中没有重复的记录(distinct)

select distinct * from score;

  查询结果按某个字段的值进行排序(order by)

select * from score order by score asc;

select * from score order by score desc;   

 

多表联合查询:

     查询数据库时,也可以将多个表的内容放在一起进行查询

select student.name from student, score where student.id = score.id and score.score < 60;

上述语句中,from关键字后出现了两个表名student和score,表示对这两个表进行联合查询。  

 

使用别名进行多表联合查询:

 可以使用表的别名以避免多次输入较长的表名,使代码更清晰

select A.name from student A, score B where A.id = B.id and B.score < 60;

    上述语句中,from关键字后的表名都指定了一个别名。

注意:别名只在当前语句中有效。

0 0
原创粉丝点击