1.别名
给查询的字段改个名字,只对查询结果有影响,对表结构以及数据不会产生任何影响
先创建一张表并添加数据
1 CREATE DATABASE mahaiwuji; 2 USE mahaiwuji; 3 CREATE TABLE student ( 4 sid INT PRIMARY KEY AUTO_INCREMENT, 5 sname VARCHAR (32), 6 sage INT 7 ) ENGINE = INNODB DEFAULT CHARSET = utf8; 8 9 INSERT INTO student(sname,sage) VALUES (‘码海无际1‘,18); 10 INSERT INTO student(sname,sage) VALUES (‘码海无际2‘,19); 11 INSERT INTO student(sname,sage) VALUES (‘码海无际3‘,20);
把sname改为studentname
1 SELECT sid,sname AS studentname,sage FROM student;
再把sage改为studentage
1 SELECT sid,sname AS studentname,sage AS studentage FROM student;
AS可以省略
1 SELECT sid,sname studentname,sage studentage FROM student;
where 后面不能用别名当筛选条件
1 -- ok 2 SELECT sid,sname studentname,sage studentage FROM student WHERE sage=18; 3 -- error 4 SELECT sid,sname studentname,sage studentage FROM student WHERE studentage=18;
2.注释
注释的内容不会被执行
单行注释
-- 要注释的内容,注意--后面有个空格
# 要注释的内容,#
1 -- INSERT INTO student(sname,sage) VALUES (‘码海无际1‘,18); 2 # INSERT INTO student(sname,sage) VALUES (‘码海无际2‘,19);
多行注释
/* 要注释的内容 */
1 /* 2 INSERT INTO student(sname,sage) VALUES (‘码海无际1‘,18); 3 INSERT INTO student(sname,sage) VALUES (‘码海无际2‘,19); 4 INSERT INTO student(sname,sage) VALUES (‘码海无际3‘,20); 5 */