Question

Given two tables T1 and T2 below, is there a way in MySQL 5.7.x to get such a list field from T1, so that none of its entries are present in T2? T1.list is a regular VARCHAR, containing T2.vals delimited by ':'

From the example, I would like to get D:E field.

T1

id list
0  A:B:C
1  D:E
2  F:G

T2

vals
A
B
C
F
G
Was it helpful?

Solution

select t1.id, t1.list
from t1
left join t2 on locate(t2.vals, t1.list)
group by t1.id, t1.list
having 0 = count(t2.vals);

fiddle

In more complex case (when a token in list may contain a token as a substring) you may use

select t1.id, t1.list
from t1
left join t2 on locate(concat(':',t2.vals,':'), concat(':',t1.list,':'))
group by t1.id, t1.list
having 0 = count(t2.vals);
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top