简体   繁体   中英

SQL SELECT * FROM tableA WHERE id IN (SELECT ids FROM tableB WHERE 1)

I'm looking for the result of this query:

SELECT * FROM procesos WHERE estatus=1 AND id_proceso IN (3,1,5,7,4,6,8)

在此处输入图像描述

But selecting the processes from the following query:

SELECT procesos FROM tipos_fichas WHERE id_tipo='1'

在此处输入图像描述

What I have tried without success is this:

SELECT * FROM procesos WHERE estatus=1 AND id_proceso IN (SELECT procesos FROM tipos_fichas WHERE id_tipo='1')

在此处输入图像描述

As you can see, it only brings me the first result... as if I could only read the first process before the comma (the 3 of: 3,1,5,7,4,6,8)

Does anyone have an idea how I can solve it?

MySQL converts the string value '3,1,5,7,4,6,8' to the integer value 3 discarding the rest of the string. That's why the rest of the values are not found. This is a silent feature of MySQL that I personally dislike.

You can use the LIKE operator to find the matches you want, as in:

select p.* 
from procesos p
join tipos_fichas t on concat(',', t.procesos, ',') 
  like concat('%,', p.id_proceso, ',%')
where p.estatus = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM