MySQL Comparative querying longtext columns
Question
I have a LONGTEXT
column that I use to store paragraphs of text. Let's say that within these paragraphs there is something like:
Text text text text
COUNT: 75 text text
text text text text
text text text text
Would it be possible to do a comparative query the small string "COUNT: 75" out of all that text?
My first thought was something like
SELECT * FROM `books`
WHERE `paragraphs` LIKE '%COUNT: >0%'
Is this even possible?
Solution
Your SELECT
will only find rows where the text contains exaclty the bit between the wildcard characters: you can't combine a LIKE
with comparative logic like that.
What you can do, though, is to strip out the relevant sections of text using a regular expression and then analyse that.
Bear in mind, though, that combining
- large amounts of text
- textual content logic
- regex
all at once will not provide the best performance! I would suggest the following:
- use a trigger to strip out a subsection of text so that you have something manageable (i.e. 50 characters or so) to work with, inserting this subtext into a separate table
- use MySql regex or fulltext functions to analyse your COUNTs
So your trigger would have something like:
select
ltrim(rtrim(substring(paragraphs, instr(paragraphs, 'count:') + 6, 10)))
from books
where instr(paragraphs, 'count:') > 0
which would get you the next 10 characters after 'count:'
, trimmed of whitespace. you could then further refine that by e.g.
select substring(text_snippet, 1, instr(text_snippet, ' ')) as count_value
from
(
select
ltrim(rtrim(substring(paragraphs, instr(paragraphs, 'count:') + 6, 10)))
as text_snippet
from books
where instr(paragraphs, 'count:') > 0
) x
where isnumeric(substring(text_snippet, 1, instr(text_snippet, ' '))) = 1
to get rows where a numerical value follows the COUNT bit.
You can then extract numerical values next to COUNT, saving them as numbers in a separate table, and then use a JOIN like this:
select b.*
from books b inner join books_count_values v
on b.id = v.books_id
where v.count_value > 0
See here
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html
for the functions at your disposal.