mysql数据查询 1.查询基础
首先创建一个测试表infor如下,添加一些数据:
create table infor(id int auto_increment primary key not null,
name varchar(10) not null,
python int,
java int,
c int,
shell int);
1.1查询所有列
select*from infor;
1.2查询指定列
多个列之间以逗号分隔:
select name,python from infor;
1.3查询任意行(limit)
查询前3行:
select*from infor limit 10;
查询第3行以后接着的4行:
1.4查询时指定别名
select name as “姓名”,c as “C语言” from infor;
其中as可以省略。
1.5合并列查询
查询每个学生的总成绩:
select name "姓名",(python+java+c+shell) "总成绩" from infor;
1.6查询时添加常量列
查询时带上班级”日月神教”,这个时候后面的as不可省略:
select name “姓名” ,c “C语言”,”日月神教” as “班级” from infor;
1.7查询时去除重复数据
为了测试,先给这个表添加字段”like”:
alter table infor add column likes varchar(4);
update infor set likes="编程" where id=1 or id=11;
update infor set likes=”游戏” where id=2 or id=10;
update infor set likes=”音乐” where id=3 or id=9;
update infor set likes=”骑行” where id=4 or id=8;
update infor set likes=”阅读” where id=5 or id=6 or id=7;
添加好之后,如下图所示:
需求:要查询学生的爱好有哪些?
select distinct likes from infor;