I want to use a list of substrings as the values for my LIKE
clause.
Consider the following tables:
Strains (s)
id name
-------------------------------------------------------
562 B6;129 (Bnip3 KO)
563 B6;129 (BNIP3 Wt) [pregnant]
564 B6;129 (BNIP3 Wt) [older than 21 days]
720 BALB/C T(x:11)38H (T38H)
721 BALB/C [older than 21 days]
Links (l)
id protocol_id strain_id
-------------------------------------------------------
1 61846 563
2 13487 564
3 79465 721
4 41699 720
Animals (a)
id group_id strain_id
-------------------------------------------------------
24 9666 563
25 9666 720
Normally I would provide a query that I've tried but this time I have nothing. Instead let me break down the steps I need to do:
- Get a list of substrings from the name of the strain for all animals under the same
group_id
.- To get the substrings, I want the strings inside
(
and)
- Something like
SELECT /* get the list of substrings */ FROM animals a LEFT JOIN strains s ON s.id = a.strain_id WHERE a.group_id = 9666
- In this example, I want the following list:
(BNIP3 Wt)
and(T38H)
- In the case that there are multiple values that are enclosed in
(
and)
, just use the last one
- To get the substrings, I want the strings inside
- Use the resulting list above for the
LIKE
clause to get the list ofprotocol_id
whose strain names contain the substrings.
My idea of a solution
SELECT l.protocol_id
FROM links l
LEFT JOIN strains s
ON s.id = l.strain_id
WHERE s.name LIKE (/* put the list here with the % for wildcards */)
The end result that I want to get is the following:
protocol_id
------------------------------
61846
13487
41699