MYSQL : Select * where IN (REPLACE(...))



我有以下请求:

SELECT value from catalog_category_entity_varchar WHERE entity_id IN
('1','2','10','47')

这个请求给出了结果。

value
ok
ok
ok
ok

以下请求

SELECT value from catalog_category_entity_varchar WHERE entity_id IN
(REPLACE("'1','2','10','47'", 'dont', 'care'))

这个请求没有给出结果,因为我认为它生成的"'1','2','10','47'"entity_id是int而不是字符串。

因此我尝试了以下请求,但仍然没有结果

SELECT value from catalog_category_entity_varchar WHERE entity_id IN
(TRIM(BOTH '"' FROM REPLACE("'1','2','10','47'", 'dont', 'care')))

我的问题是,如何生成第一个带有替换的请求?谢谢大家抽出时间。

编辑:数据示例:

CREATE TABLE catalog_category_entity_varchar (entity_id int(10), value varchar(255));
INSERT INTO catalog_category_entity_varchar VALUES (1,'ok'),(2,'ok'),(10,'ok'),(47,'ok');

我试图实现的最初请求给出的结果

SELECT value from catalog_category_entity_varchar WHERE entity_id
IN ( CONCAT('"', REPLACE('1/2/10/47', '/', '","'), '"') );

您可以使用函数find_in_set():

set @entity = '1/2/10/47';
select * from catalog_category_entity_varchar
where find_in_set(entity_id, replace(@entity, '/', ','))

请参阅演示
结果:

| entity_id | value |
| --------- | ----- |
| 1         | ok    |
| 2         | ok    |
| 10        | ok    |
| 47        | ok    |

最新更新