第4章: 检索数据
Tips: 关键字不区分大小写,数据库名表名区不区分大小写有相关的数据库配置。
select xx from xx;
select xx,yy,zz from xx;
select * from xx;
select distinct xx from xx;
select distinct xx,yy from xx; # 指xx,yy的组合不同
select xx from xx limit 5;
select xx from xx limit 3,5; # 从第3行开始,显示5行,行号从0开始
select xx from xx limit 5 offset 3; # 行号从0开始,从行3开始的5行
select tblname.colname from dbname.tblname; # 完全限定的表名、列名
第5章: 排序检索数据
select
、from
、order by
、limit
这些都是子句
select xx from xx order by xx;
select xx from xx order by xx,yy,zz; # 先按xx排序,xx值一样的再按yy排序...
select xx from xx order by xx desc,yy asc,zz desc; # asc递增,desc递减,默认asc递增,这里指先按xx值递减排序,xx值相同的再按yy值递增排序...
select xx from xx order by xx limit x;
第6章:过滤数据
select xx from xx where xx=xx;
select xx from xx where xx<>xx;
select xx from xx where xx !=xx;
select xx from xx where xx between xx and yy;
select xx from xx where xx is null;
select xx from xx where xx>xx;
如:where age=18
、 where name='lcheng'
(带引号,表示字符串)
如果列为空(null
),则既不属于匹配也不属于不匹配,用“等于”或者“不等于”过滤的时候要注意,结果中不会返回为null的行。
select xx from xx where xx=xx order by xx limit xx;
第7章:数据过滤
select xx from xx where xx=xx and yy=yy;
select xx from xx where xx=xx or yy=yy;
select xx from xx where xx=xx or xx=yy and zz=zz; # and具有高优先级,为避免歧义,一律用括号括起来,不要依赖默认,即 select xx from xx where (xx=xx or xx=yy) and zz=zz;
select xx from xx where xx in (xx,xx,xx,xx);
not in
、not between
、not exists
, 如:
select xx from xx where xx not in (xx,xx,xx,xx)
select xx from xx where xx not between xx and yy;