首页 > 试题广场 >

假设有一张账目表account,里面记录着你今年的每一笔开支

[单选题]
假设有一张账目表account,里面记录着你今年的每一笔开支,目前你想查看一下3月和7月这两个月份哪个月份的消费总额最高,那么下面正确的sql语句是
create table `account`(
    `id` int(11) not null auto_increment,
    `month` int(11) not null comment '月份',
    `item` char(50) not null comment '开支条目',
    `pay` int(11) not null comment '消费金额',
    primary key(`id`)
)engine = innodb;

  • select month,count(pay) c from account where month between 3 and 7 group by month order by c desc limit 1
  • select month,sum(pay) s from account where month = 3 and month = 7 group by month order by s desc limit 1
  • select month,count(pay) c from account where month = 3 or month=7 group by month order by c desc limit 1
  • select month,sum(pay) s from account where month in (3,7) group by month order by s desc limit 1
答案是:D

首先需要查询消费总额,需要用到的计算总和函数 sum() 而不是统计总数的函数 count() ,排除选项A和C,其次需要查询3月和7月两个月的数据进行对比,WHERE查询条件应当为month = 3 OR month = 7或者month in (3,7),排除选项A和B,故选D。
发表于 2022-08-05 15:47:01 回复(0)