select
关键词:select,as
返回一个二元表。后面跟字符类型或运算类型,字符类型。运算类型运算式做字段(表名),运算结果为一条数据。字符类型字段和数据同名。
as可以为给字段起别名(可省略)。省略和和不省略注意引号问题。
mysql>select 2*7;
+-----+
| 2*7 |
+-----+
| 14 |
mysql>select '青柠檬的';
+----------+
| 青柠檬的 |
+----------+
| 青柠檬的 |
+----------+
mysql>select '青柠檬的' as 奶茶店; #引号对别名不影响
+----------+
| 奶茶店 |
+----------+
| 青柠檬的 |
+----------+
msyql>select '青柠檬的' 奶茶店; #注意下面两条语句的结果
+----------+
| 奶茶店 |
+----------+
| 青柠檬的 |
+----------+
mysql>select '青柠檬的' '奶茶店';
+----------------+
| 青柠檬的 |
+----------------+
| 青柠檬的奶茶店 |
from
关键字:from,select
后面接要查的表(可以接多个表名),与select配合,查询单表时返回整张表的数据,查询多表时返回的为多表的笛卡尔积。
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | frank |
| 2 | pony |
+------+-------+
mysql> select * from t2;
+------+---------+
| math | english |
+------+---------+
| 87 | 99 |
| 67 | 89 |
+------+---------+
mysql> select * from t1,t2;
+------+-------+------+---------+
| id | name | math | english |
+------+-------+------+---------+
| 1 | frank | 87 | 99 |
| 2 | pony | 87 | 99 |
| 1 | frank | 67 | 89 |
| 2 | pony | 67 | 89 |
+------+-------+------+---------+
dual
关键词:select,from,dual
跟在from后面的关键字,为mysql中的虚拟表。select…from…后面其实时省略了as duanl。
mysql>select 2*7 as res;
+-----+
| res |
+-----+
| 14 |
+-----+
mysql>select 2*7 as res from dual;
+-----+
| res |
+-----+
| 14 |
+-----+
where
后面筛选条件
in
和 or(not in)结果是等效的。但是两者查询效率上有差别。
between and
查询区间为左闭右闭。[ ]
is null
select * from students where id is null;
查询值是否为空
聚合函数
like模糊查询
select * from student where name like '张%' #%代表多个值
select * from student where name like '张_' #_代表一个值
order by
select * from score order by chinese asc; #升序
select * from score order by chinese desc; #降序
group by
查询的字段必须为聚合函数和分组字段。
select avg(age) as '年龄',gender as '性别' from info group by gender; #按性别去分组,group by后面跟分组条件
select avg(age) as '年龄',address as '地区' from info group by address; #按地区分组
group_concat
select group_concat(name),gender from from student group by gender;
having
对已经查询过后的结果在进行查询。
select chinese from score where chinese>95
select avg(age) as 'age', address as '地区' from info group by address
having age>24;
limit
限制
select * from limit 0,2; -- [起始量,偏移量)
distinct
去除重复的信息
select distinct address from info;
select count(distinct address) from info;
select all address from info == select address from info;