learning_record_doc/mysql/MySQL踩坑记录.md
2022-10-21 17:02:22 +08:00

29 lines
867 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## MySQL踩坑记录
### 【1235】 This version of MySQL doesnt yet support LIMIT & IN/ALL/ANY/SOME subquery
+ ##### 说明:当前版本的 MySQL 不支持使用 LIMIT 子句的 IN/ALL/ANY/SOME 子查询
+ ##### 问题SQL
我想要查找凭证列表中的前十条记录
```mysql
SELECT * FROM voucher where id IN
(select DISTINCT(id) from voucher where is_deleted = 0 and DATE_FORMAT(v.bookkeeping_date,'%Y-%m') >= '2022-08' LIMIT 0,10)
```
+ ##### 原因
+ 原因在于子查询三层嵌套在 in或 not in 中使用了 limit
+ ##### 解决办法
将我们的SQL在进行嵌套一层绕开limit子查询就可以了
```mysql
SELECT * from voucher where id IN
(select id from (select DISTINCT(id) from voucher where is_deleted = 0 and DATE_FORMAT(v.bookkeeping_date,'%Y-%m') >= '2022-08' LIMIT 0,10) vtable)
```