Compare Texts in mysql

0

I need to make an Insert to my database, my table is called "Topic" which keeps an idTema, textTeam and date. The point is that when I want to insert a topic, I must check if a similar theme already exists (in other words). I'm doing something like this ...

select * from tema where textoTema like CONCAT('%', '".$variableConTexto."', '%');

This query returns me null ... Is there any other function to do it? that is effective, since the previous query when testing it with a single word or letter is perfectly executed.

PS: I'm using codeigniter

    
asked by Elias Galarza 26.12.2017 в 18:40
source

3 answers

0

Try this query

SELECT * FROM tema WHERE textoTema LIKE '% $variableConTexto%'

This should bring all the matches with the text that is in $ variableContext.

    
answered by 26.12.2017 в 19:00
0

Good, have you tried something like that?

SELECT * FROM tema WHERE textoTema LIKE "%".$variableConTexto."%";
    
answered by 26.12.2017 в 19:02
0

You can use an index of type FULLTEXT on the field you want to search and MySQL will return an approximate answer of the fit between text strings even if one does not contain exactly the other:

create table palabras (
    palabra text,
    FULLTEXT idx (palabra)
) ENGINE=InnoDB;

insert into palabras (palabra) values ('casa'),
('casita'),
('mi casa'),
('tres casas'),
('perro'),
('alfombra'),
('mesa'),
('cama'),
('tejado'),
('cocina'),
('gato');

Existing the FULLTEXT index, you can use the MATCH function.

SELECT palabra 
FROM palabras
WHERE MATCH(palabra) AGAINST('*una casa*')

And it will give you the rows casa and mi casa .

Sample Fiddle .

For this to work, you will need at least three rows in your table. Otherwise there is not a sufficient prevalence of words to decide whether the text looks like or not

You could also combine the predicates guarding for that edge case:

SELECT palabra 
FROM palabras
WHERE MATCH(palabra) AGAINST('*una casa*')
OR palabra LIKE '%una casa%'
    
answered by 26.12.2017 в 23:38