So I have a table that looks like this:
cid | tid | score
-----+-------+-------
6 | 383 | 88
2 | 234 | 91
3 | 685 | 77
5 | 543 | 97
etc...
What I am trying to do is find an ordinal field pattern in the tid column, then search for other instances of that pattern in the same column. For example:
I query to display all tids of a single cid, ordered by descending score:
cid | tid | score
-----+-------+-------
6 | 909 | 100
6 | 119 | 99
6 | 221 | 98
6 | 765 | 97
etc...
A pattern is established in the tid column: 909, 119, 221, 765. Now I want to find other possible instances of that pattern, where the cid value is something else. (Note: different cids can have the same tids, so cid 6 can have tid 909, and cid 4 can too.)
I want to check for the pattern in a second query. The pattern values can be spaced out, but they must appear in order:
QUERY 2:
cid | tid | score cid tid score
--------------------- ---------------------
6 | 909 | 100 5 | 909 < | 100
6 | 119 | 99 5 | 831 | 97
6 | 221 | 98 5 | 793 | 96
6 | 765 | 97 5 | 435 | 96
5 | 404 | 95
5 | 119 < | 94
5 | 221 < | 94
5 | 765 < | 94
After the comparison, I would like MySQL to return something like:
cid | tid pattern | instances (in order)
----+---------------+----------------------
5 909 1 (or 0)
5 119 1 (or 0)
5 221 1 (or 0)
5 765 1 (or 0)
Is there a reasonable way to do this in MySQL? Any help appreciated.
Gnerally this kind of operation doesn’t work well across rows – Start by pulling it into a single record per cid:
Then you can easily get a list of all combinations where the patterns match or are a superset:
You might want to have a look at levenstein functions or convolution (hint: if you’re not analysing genomes then have a look at some code which does).