You can try with the following regex syntax:
SELECT @arr2 REGEXP CONCAT('(^|,)', REPLACE(@list1, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list2, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list3, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list4, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list5, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list6, ',', ',([0-9]+,)*'), '(,|$)'),
@arr2 REGEXP CONCAT('(^|,)', REPLACE(@list7, ',', ',([0-9]+,)*'), '(,|$)')
Basically replaces the commas with a regex that matches at least a comma, and any combination of numbers followed by a comma, then encloses this between:
- the start of string or comma
(^|,)
- the end of string or comma
(,|$)
In this way arr2 should contain all numbers of the lists, and any other number.
The output is 0 when the elements of the list are not found in arr2, and 1 when they are. If you want to get 'YES' and 'NO', it's sufficient to include the result of the REGEXP operation inside an IF
statement:
IF(@arr2 REGEXP CONCAT(...) = 1, 'YES', 'NO')
Hence replace variable names with the corresponding columns you have in your table.
Check the demo here.