最简单的单表查询 : select * from tb_name;
*表示,所有字段
查询特定字段(投影):
select 字段名1,字段名2, from tb_name;
where 语句过滤查询(选择)
select * from tb_name where 条件 ;
使用SELECT子句进行多表查询
SELECT 字段名 FROM 表1,表2 … WHERE 表1.字段 = 表2.字段 AND 其它查询条件
SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id
注:在上面的的代码中,以两张表的id字段信息相同作为条件建立两表关联,但在实际开发中不应该这样使用,最好用主外键约束来实现
方法一:
mysql> select students.number,students.name,students.sex,course.math,course.english,course.chinese
-> from students inner join course
-> on students.number=course.number;
方法二:
mysql> select students.number,students.name,students.sex,course.math,course.english,course.chinese
-> from students,course
-> where students.number=course.number;
mysql> select students.number as 学号,students.name as 姓名,course.math as 数学,course.english as 英语,course.chinese as 语文 from students,course where students.number=course.number order by students.number;
mysql> select students.number as 学号,students.name as 姓名,(course.math+course.english +course.chinese) as 总成绩 from students,course where students.number=course.number order by 总成绩 desc;