MySQL/Oracle用逗号分割的id怎么实现in (逗号分割的id字符串)。find_in_set(`id`, ‘1,2,3‘) 函数,

1.MySQL

1.1.正确写法

sql 复制代码
select * from student where find_in_set(`s_id`, '1,2,3');  

1.2.错误示范

sql 复制代码
select * from student where find_in_set(`s_id`, '1,2 ,3');     -- 注意,中间不能有空格。1、3
select * from student where find_in_set(`s_id`, '1,2, 3');     -- 注意,中间不能有空格。1、2
select * from student where find_in_set(`s_id`, '1,2 , 3');    -- 注意,中间不能有空格。1

2.Oracle

2.1.方式一

sql 复制代码
select * 
from student 
where s_id in
	(
	select regexp_substr('1,2,3', '[^,]+', 1, level) as value
	from dual
	connect by regexp_substr('1,2,3', '[^,]+', 1, level) is not null
	);

2.2.方式二

sql 复制代码
-- 在Oracle SQL中,没有内置的FIND_IN_SET函数,但是你可以使用一些技巧来实现类似的功能。以下是一个示例:

SELECT *
FROM student
WHERE ',' || '1,2,3' || ',' LIKE '%,' || s_id || ',%';
相关推荐
Hoffer_8 小时前
MySQL 强制索引:USE/FORCE INDEX 用法与避坑
后端·mysql
Hoffer_8 小时前
MySQL 索引核心操作:CREATE/DROP/SHOW
后端·mysql
随风飘的云1 天前
MySQL的慢查询优化解决思路
数据库
IvorySQL1 天前
PostgreSQL 技术日报 (3月7日)|生态更新与内核性能讨论
数据库·postgresql·开源
赵渝强老师1 天前
【赵渝强老师】金仓数据库的数据文件
数据库·国产数据库·kingbase·金仓数据库
随逸1771 天前
《Milvus向量数据库从入门到实战,手把手搭建语义检索系统》
数据库
神秘的猪头1 天前
🚀 React 开发者进阶:RAG 核心——手把手带你玩转 Milvus 向量数据库
数据库·后端·llm
0xDevNull2 天前
MySQL索引进阶用法
后端·mysql
0xDevNull2 天前
MySQL索引用法
mysql
IvorySQL2 天前
PostgreSQL 技术日报 (3月6日)|为什么 Ctrl-C 在 psql 里让人不安?
数据库·postgresql·开源