1. 修改Customer表中客户姓名是“李立”的联系方式为15119280983
UPDATE customer SET phone = 15119280983 where custname='李立';
2. 查询出能够容纳12人以上的最合适的包厢信息。
select * from box where boxcapacity >= 12 and boxstatus = '空闲'
3. 查询可容纳人数最多的包厢信息。
select * from box where boxcapacity=(select max(boxcapacity) from box);
select * from box ORDER BY boxcapacity desc LIMIT 1;
第一句是将一条查询语句的结果作为参数加进主句的where里做判断
这是两种方法,更推荐第二种方法
4. 查询容纳人数按照从多到少排第三的包厢信息。
select * from box
ORDER BY boxcapacity desc
limit 2,1;
select box.* from (select * from box ORDER BY boxcapacity desc limit 3) box
order by box.boxcapacity asc limit 1;
limit 2,1 :第二行的位置取一行
5. 查询所有会员的平均年龄。
select Round(AVG(year(now())-year(birthday))) as avgAge
from customer
where ismembership = "是";
year:获取年份函数
AVG:平均函数
Round:四舍五入函数
6. 查询非会员中年大于30周岁的人员信息。
select * from customer
where ismembership = "否" and (year(now())-year(birthday)) > 30;
7. 查询所有客户的姓名,结果不包含重复记录。
select custname from customer GROUP BY custname;
group by对客户名称做了分组
8 将所有客户的姓名中的“张”替换为“章”。
select replace(sname,'张','章') from student;
replace:替换字方法
9 查询包厢容量大于等于30的包厢信息的包厢名称和底价。
select boxname a,lowestprice b from box where boxcapacity>=30;
a,b:给比较长的字段起个外号
10 将结果中列的标题分别指定为“包厢名”和“最低价”。
select boxname as 包厢名,MIN(lowestprice) as 最低价 from box;
MIN:最小值
as:修改当前字段展示字段名