mysql统计查询语句_mysql查询语句举例

select stuid, name, sum(score) as sum_score from stuscore group by stuid order by sum_score desc;

select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by stuid);

select stuid, name, sub, score from stuscore where score in (select max(score) from stuscore group by sub);

select a.* from stuscore a where exists (select count(*) from stuscore where sub = a.sub and score > a.score having count(*) < 2) order by a.sub, a.score desc;

select sub, avg(score) as avg_score from stuscore group by sub order by avg_score desc;

select (select (count(stuid)+1 from (select stuid, sum(score) as sum_score from stuscore group by stuid) as A where A.sum_score > B.sum_score) as seq, B.stuid, B.name, B.sum_score from (select stuid, name, sum(score) as sum_score from stuscore group by stuid)as B order by sum_score desc;

select stuid, name, score, sub from stuscore where sub = 'math' order by score desc limit 1, 3;

select (select (count(stuid)+1 from (select stuid, score from stuscore where sub = 'math') as A where A.score > B.score) as seq, B.stuid, B.name, B.sum_score from (select stuid, name, sub, score from stuscore where sub = 'math' and name = '李四')as B;

select stuid, name, sum(case when sub = 'chinese' then score else 0 end) as chinese, sum(case when sub = 'math' then score else 0 end) as math, sum(case when sub = 'english' then score else 0 end) as english, sum(score) as sum_score, avg(score) as avg_score from stuscore group by stuid;

select sub, sum(case when score < 60 then 1 else 0 end) as lower_60, sum(case when score < 81 and score > 59 then 1 else 0 end) as between_60_80, sum(case when score > 80 then 1 else 0 end) as higher_80 from stuscore group by sub;

好的,以下是一个复杂的MySQL查询语句: ``` SELECT customers.customer_name, orders.order_date, SUM(order_items.quantity * order_items.unit_price) AS total_spent FROM customers JOIN orders ON customers.customer_id = orders.customer_id JOIN order_items ON orders.order_id = order_items.order_id WHERE orders.order_date BETWEEN '2020-01-01' AND '2020-12-31' GROUP BY customers.customer_id HAVING total_spent > 1000 ORDER BY total_spent DESC; ``` 这个查询语句的目的是为了找到在2020年购买金额超过1000的顾客,并按照花费金额从高到低进行排序。以下是对这个查询语句的解释: 首先,我们使用SELECT语句来选择我们要查询的列,包括顾客名称、订单日期和总共花费的金额。使用SUM函数来计算每个订单的总花费,并将其重命名为total_spent。 然后,我们使用FROM关键字并指定需要查询的表。我们使用JOIN语句将三个表连接起来,以便我们可以检索到每个顾客的订单,以及每个订单的商品。 接下来,我们使用WHERE语句过滤掉不符合条件的订单。在这个例子中,我们只想要在2020年1月1日至12月31日之间的订单。 然后,我们使用GROUP BY子句来分组相同的顾客,并对它们的订单进行总花费的汇总。这个子句将返回每个顾客的总花费。 接下来,我们使用HAVING子句来过滤掉总花费低于1000的顾客。 最后,我们使用ORDER BY子句按照总花费的大小进行排序,并将结果从高到低进行排列。 总之,这个查询语句可以帮助我们找到在特定时间段内花费最多的顾客,并且可以为我们提供有关他们购买习惯的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值