SQL中SELECT语句常用方法

原始表

数据库名字是db_students,表名字是tb_scores

id name course score time
1 carol English 98 2019-12-19 04:00:49
2 Ann English 78 2019-12-19 04:00:49
3 张三 English 50 2019-12-19 04:00:49
4 王二 English 45 2019-12-19 04:00:49
5 Carol Math 77 2019-12-19 05:00:49
6 Ann Math 99 2019-12-19 05:00:49
7 张三 Math 67 2019-12-19 05:00:49

Select * from table name

搜索整张表内容,以下两句都能实现,第一句必须在db_students数据库下,第二句在当前服务器下就可以了。

select * from tb_scores;
select * from db_students.tb_scores;
id name course score time
1 carol English 98 2019-12-19 04:00:49
2 Ann English 78 2019-12-19 04:00:49
3 张三 English 50 2019-12-19 04:00:49
4 王二 English 45 2019-12-19 04:00:49
5 Carol Math 77 2019-12-19 05:00:49
6 Ann Math 99 2019-12-19 05:00:49
7 张三 Math 67 2019-12-19 05:00:49

Select * from table name where 条件语句

  • 搜索数据id=1的数据
select * from db_students.tb_scores where id=1;
id name course score time
1 carol English 98 2019-12-19 04:00:49
  • 搜索score>60的数据
id name course score time
1 carol English 98 2019-12-19 04:00:49
2 Ann English 78 2019-12-19 04:00:49
5 Carol Math 77 2019-12-19 05:00:49
6 Ann Math 99 2019-12-19 05:00:49
7 张三 Math 67 2019-12-19 05:00:49
  • 搜索time介于 2019-12-18 04:00:00和 2019-12-19 05:00:00的数据
select * from db_students.tb_scores where time between '2019-12-18 04:00:00' and '2019-12-19 05:00:00';
id name course score time
1 carol English 98 2019-12-19 04:00:49
2 Ann English 78 2019-12-19 04:00:49
3 张三 English 50 2019-12-19 04:00:49
4 王二 English 45 2019-12-19 04:00:49
  • 模糊查询name含有a的数据
select * from db_students.tb_scores where name like '%a%';
id name course score time
1 carol English 98 2019-12-19 04:00:49
2 Ann English 78 2019-12-19 04:00:49
5 Carol Math 77 2019-12-19 05:00:49
6 Ann Math 99 2019-12-19 05:00:49

未完

猜你喜欢

转载自blog.csdn.net/sinat_37960022/article/details/114980784