[英]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', '/', '","'), '"') );
您可以使用 function 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 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.