Mysql函数使用技巧

1、数据去重

高级且优雅的自连接

输入:
Person 表:
±—±-----------------+
| id | email |
±—±-----------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
±—±-----------------+
输出:
±—±-----------------+
| id | email |
±—±-----------------+
| 1 | john@example.com |
| 2 | bob@example.com |
±—±-----------------+
解释: john@example.com重复两次。我们保留最小的Id = 1。
链接:https://leetcode.cn/problems/delete-duplicate-emails

DELETE p1 
FROM Person p1, Person p2
WHERE p1.Email = p2.Email AND p1.Id > p2.Id

2、列转行

输入:
Products table:
±-----------±-------±-------±-------+
| product_id | store1 | store2 | store3 |
±-----------±-------±-------±-------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
±-----------±-------±-------±-------+
输出:
| ------------ | -------- |-------|
| product_id | store | price |
±-----------±-------±------+
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
±-----------±-------±------+
解释:
产品0在store1,store2,store3的价格分别为95,100,105。
产品1在store1,store3的价格分别为70,80。在store2无法买到。
链接:https://leetcode.cn/problems/rearrange-products-table

select product_id, 'store1' store, store1 price  from products where store1 is not null
union
select product_id, 'store2' store, store2 price  from products where store2 is not null
union
select product_id, 'store3' store, store3 price  from products where store3 is not null

3、行转列

输入:
Products table:
±-----------±-------±------+
| product_id | store | price |
±-----------±-------±------+
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
±-----------±-------±------+

输出:

| product_id | store1 | store2 | store3 |
±-----------±-------±-------±-------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
±-----------±-------±-------±-------+

SELECT product_id,
  SUM(IF(store = 'store1', price, NULL)) 'store1',
  SUM(IF(store = 'store2', price, NULL)) 'store2',
  SUM(IF(store = 'store3', price, NULL)) 'store3' 
FROM
  Products1 
GROUP BY product_id ;

4、对日期的一些处理(函数)

1、在一个时间上加上一个时间

例如:在‘2017-10-10’加上10天

select date_add("2017-10-10", interval 10 day);  
->2017-10-20

在‘2017-10-10 15:25:18’加上15分钟

select date_add("2017-10-10 15:25:18", interval 15 minute);
->2017-10-10 15:40:18

2、两日期之间的差值

例如:2001-01-01与2001-02-02相差多少天

select datediff('2001-01-01','2001-02-02');
->-32
#说明:这相当于前一个日期减去后一个日期,可以为负也可以为正

5、模糊查询,匹配查询

经常在各个博客上看到说模糊查询会使索引失效,类似 like ‘关键词%’ 不会失效,但是like ‘%关键词%’ 会失效。然后就有使用全文检索的说法,但是全文检索只支持myisam、并且不支持联表查询。于是对于这种需求应该怎么做才能走索引,或者说怎么做才能使查询更快?

explain SELECT * FROM user where username like '%ab%'

在这里插入图片描述

explain select * from user where instr(username, 'ab')

在这里插入图片描述

explain select * from user where locate('ab', username)

在这里插入图片描述

可以看到使用上面三个sql执行结果分析不同在于filtered,对于这个列的解释为越大越好。所以可以得出结论,使用函数比使用like + %的性能 要好,至于走不走索引就不得而知了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL聚合函数是一组用于计算行数据的函数,它们返回单个值作为结果。常见的聚合函数包括SUM、AVG、COUNT、MAX和MIN。下面是一些高级使用聚合函数技巧: 1. 使用DISTINCT关键字 在聚合函数使用DISTINCT关键字可以消除重复值。例如,如果您想计算不同城市中有多少个客户,可以这样写: ``` SELECT COUNT(DISTINCT city) FROM customers; ``` 2. 使用GROUP BY子句 GROUP BY子句将行分组为多个集合,并对每个组应用聚合函数。例如,如果您想找出每个城市的平均客户数,可以这样写: ``` SELECT city, AVG(customers) FROM sales GROUP BY city; ``` 3. 使用HAVING子句 HAVING子句与WHERE子句类似,但它用于过滤GROUP BY子句生成的结果集。例如,如果您想找出平均客户数大于10的城市,可以这样写: ``` SELECT city, AVG(customers) FROM sales GROUP BY city HAVING AVG(customers) > 10; ``` 4. 使用ORDER BY子句 ORDER BY子句用于按升序或降序对结果集进行排序。例如,如果您想按平均客户数降序排列城市,可以这样写: ``` SELECT city, AVG(customers) FROM sales GROUP BY city ORDER BY AVG(customers) DESC; ``` 5. 使用聚合函数嵌套 聚合函数可以嵌套在其他聚合函数中。例如,如果您想找出每个城市的最大客户数,可以这样写: ``` SELECT city, MAX(customers) FROM (SELECT city, COUNT(*) AS customers FROM sales GROUP BY city) AS subquery GROUP BY city; ``` 以上是一些高级使用聚合函数技巧,它们可以帮助您更好地处理和分析数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值