delete from stock_data where tenant_code = 'TRENDY' and bus_type>=15000 and kqqs ='202305' and user_id in (select user_id from stock_data a where type='leave_01' and deduct_id not in (select id from stock_data b where type='leave_01') and bus_type !=10 group by user_id); 在mysql里面有语法错误,如何改进
时间: 2024-03-09 18:46:39 浏览: 75
fusionpbx-4.2.zip_freeswitch_full_fusionpbx 4.2_multi-tenant_pbx
可以尝试将子查询中的 `NOT IN` 改为 `NOT EXISTS`,同时使用 `JOIN` 替代子查询,如下所示:
```
DELETE FROM stock_data
WHERE tenant_code = 'TRENDY'
AND bus_type >= 15000
AND kqqs = '202305'
AND user_id IN (
SELECT a.user_id
FROM stock_data a
LEFT JOIN stock_data b ON a.deduct_id = b.id AND b.type = 'leave_01'
WHERE a.type = 'leave_01'
AND b.id IS NULL
AND a.bus_type != 10
GROUP BY a.user_id
);
```
这样可以避免 `NOT IN` 子查询的效率问题,并且使用 `JOIN` 可以提高查询效率。同时,将子查询中的条件移动到 `JOIN` 子句中,可以避免不必要的全表扫描,从而提高查询性能。
阅读全文