显示表格中一个或数个栏位的所有资料
语法:SELECT “栏位” FROM “表名”;
select * from test1;
select name from test1;
select name,sex from test1;
不显示重复的内容
语法:SELECT DISTINCT “栏位” FROM “表名”;
select distinct age from test1;
语法:SELECT “栏位” FROM “表名” WHERE “条件”;E
select name,age from test1 where age > 21;
select name,age from test1 where name='nancy';
代表且、或、和的意思
语法:SELECT “栏位” FROM “表名” WHERE “条件1” {[AND|OR] “条件2”};
select * from test1 where sex='女' and age > 21;
select * from test1 where age > 21 or age < 20;
select * from test1 where (age > 21 and sex='男') or age < 20;
显示已知的值的内容
语法:SELECT “栏位” FROM “表名” WHERE “栏位” IN (‘值1’, ‘值2’, …);
select * from test1 where age in (19,20,21);
显示两个值范围内的资料
语法:SELECT “栏位” FROM “表名” WHERE “栏位” BETWEEN ‘值1’ AND ‘值2’;
select * from test1 where age between 19 and 21;
匹配一个模式来找出我们要的资料
语法:SELECT “栏位” FROM “表名” WHERE “栏位” LIKE {模式};
select * from test1 where name like '%cy';
select * from test1 where name like '%lu%';
select * from test1 where name like 'l_l%';
按关键字排序
语法:SELECT “栏位” FROM “表名” [WHERE “条件”] ORDER BY “栏位” [ASC, DESC];
#ASC 是按照升序进行排序的,是默认的排序方式。
#DESC 是按降序方式进行排序。
select * from test1 where name='kate' order by age;
select * from test1 where name='kate' order by age desc;
select abs(-10),rand(),mod(10,3),power(2,10),round(3.1415);
select round(3.1415926,3),sqrt(9),truncate(3.1415926,3),ceil(3.5),floor(1.11),greatest(1,2,3,4),least(1,2,3,4);
select avg(age) from test1;
select count(age) from test1;
select min(age) from test1;
select max(age) from test1;
select sum(age) from test1;
count(*) 表示包括所有列的行数,不会忽略null值
count(列名) 表示只包括这一列,统计时会忽略null值的行
select trim(' abc ');
select (' abc ');
select concat(name,age) from test1 where id=1;
select concat(name,' ',age) from test1 where id=1;
#如sql_mode开启 了PIPES_AS_CONCAT, “||” 视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一-样的
select name || age from test1 where id=1;
select name || ' ' || age from test1 where id=1;
substr(x,y)与substr(x,y,z)——截取字符串
select substr(name,3) from test1 where id=3;
select substr(name,2) from test1 where id=3;
select substr(name,2,2) from test1 where id=3;
select length(name) from test1 where id=3;
select replace(name,'kate','ketty') from test1 where id=3;
select upper(name) from test1 where id=3;
select lower(name) from test1 where id=3;
select left(name,2) from test1 where id=3;
select right(name,2) from test1 where id=3;
select repeat(name,2) from test1 where id=3;
select space(2) from test1 where id=3;
select strcmp(10,20);
select reverse(123456);
SELECT TRIM ([位置] [要移除的字符串] FROM 字符串);
#[位置]:的值可以为LEADING(起头),TRAILING(结尾),BOTH(起头及结尾)
#[要移除的字符串]:从字串的起头、结尾、或起头及结尾移除的字符串,缺省时为空格
select trim(leading 'na' from 'nancy');
select trim(trailing 'cy' from 'nancy');
select trim(both 'n' from 'nancin');
对GROUP BY 后面的查询结果进行汇总分组,通常是结合聚合函数一起使用
GROUP BY有个原则,select 后面的所有列中,没有用聚合函数的列,必须出现在GROUP BY后面
语法:SELECT "栏位1", SUM("栏位2") FROM "表名" GROUP BY "栏位1";
select name,sum(age) from test1 group by name;
select name,sum(age) from test1 group by name order by age;
select name,age from test1 group by name order by age;
select name,age from test1 group by name,age order by age;
用来过滤由GROUP BY语句返回的记录集,通常与GROUP BY语句联合使用
HAVING的存在弥补了WHERE关键字不能和聚合函数联合使用的不足,如果被select的只有函数栏,那就不需要GROUP BY子句
语法:SELECT “栏位1”, SUM(“栏位2”) FROM “表格名” GROUP BY “栏位1” HAVING (函数条件);
select name,sum(age) from test1 group by name order by age;
select name,sum(age) from test1 group by name having sum(age) > 30;
HAVING只是条件过滤,用来筛选前面GROUP BY的结果
栏位別名 表格別名
语法:SELECT “表格別名”.“栏位1” [AS] “栏位別名” FROM “表格名” [AS] “表格別名”;
select name,age,tot.sex xb from test1 tot group by age;
7.1 inner join(等值相连)
只返回两个表中联结字段相等的行
select * from test6 A inner join test1 B on A.name = B.name;
7.2 left join(左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
select * from test6 A left join test1 B on A.name = B.name;
7.3 right join(右联接)
返回包括右表中的所有记录和左表中联结字段相等的记录
select * from test6 A right join test1 B on A.name = B.name;
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
语法:SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符] #外查询
(SELECT "栏位1" FROM "表格2" WHERE "条件"); #内查询
可以是符号的运算符,例如 =、>、<、>=、<=;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
先进行内查询,查询结果再交给外查询进行进一步过滤
select name from test1 where age=21;
select sum(age) from test6 where name in ('lucy','kate');
select sum(age) from test6 where name in (select name from test1 where age=21);
select A.age,A.name from test6 A where A.name in (select name from test1 B where B.name=A.name);
用来测试内查询有没有产生任何结果,类似布尔值是否为真
如果有的话,系统就会执行外查询的SQL语句,若是没有的话,那整个SQL语句就不会产生任何结果
语法:SELECT “栏位” FROM “表格1” WHERE EXISTS (SELECT * FROM “表格2” WHERE “条件”);
select sum(age) from test6 where exists (select name from test1 where age=21);
select sum(age) from test6 where exists (select name from test1 where age=25);