Slinidy
Slinidy

Reputation: 387

Where with multiples like

I'm trying to check if an array of numbers has these numbers inside another array in MySQL using where like

Example (SQL Fiddle):

My table:

id columnNumbers
1 2,41
2 1,21,35
3 5,10,55
const numbers = [1, 41]
db.query(`select * from table where concat(",", columnNumbers, ",") like ${numbers.map(n => `"%,${n},%"`).join(' or ')}`)

Output:

id columnNumbers
2 1,21,35

Expected output:

id columnNumbers
1 2,41
2 1,21,35

MySQL version:

  1. SQL Fiddle: 5.6
  2. MySQL Workbench: 8.0.23

Upvotes: 1

Views: 37

Answers (1)

Akina
Akina

Reputation: 42728

select * 
from _table 
where FIND_IN_SET(1, columnNumbers)
   or FIND_IN_SET(41, columnNumbers)

Upvotes: 1

Related Questions