本文共 1477 字,大约阅读时间需要 4 分钟。
select * from info;
select Code as '代号', name as '姓名' from info;
select * from car where code = 'c002';
select * from car where brand = 'b001' or power = 130;
select * from car where name like '%奥迪%';
注意:%
表示任意多个字符,_
表示单个字符。
select * from car order by brand, power desc;
默认排序为升序,不需要写asc
。
select * from car where price between 40 and 60;
select * from car where code in('coo1','c003','c005','c007');
select * from car where code not in('coo1','c003','c005','c007');
select count(code) from car; -- 查询记录数
select sum(price) from car; -- 查询总和
select max(code) from car; -- 查询最大值
select min(brand) from car; -- 查询最小值
select avg(price) from car; -- 查询平均值
select year(brithday) from dtudent; -- 提取生日的年份
select * from Car limit (n-1)*5,5; -- 取第n页,每页显示5条
select distinct Brand from Car;
select Brand, count(*) from Car group by Brand; -- 按品牌分组并统计记录数
select Brand from Car group by Brand having count(*) > 3; -- 分组后按条件筛选
select Info.Code, Info.Name, Info.Sex, Nation.Name, Info.Birthday from Info, Nation where Info.Nation = Nation.Code;
select Code, Name from Infounionselect Code, Name from Nation;
select * from Info where Nation = (select Code from Nation where Name = '汉族');
select * from Car a where Oil < (select avg(Oil) from Car b where b.Brand = a.Brand);
group by
时,注意having
语句进行筛选。like
查询中,%
和_
的使用需要谨慎,避免过度匹配。转载地址:http://jqbfk.baihongyu.com/