Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL WHERE using LIKE and comparing to a field

Tags:

sql

mysql

I have this scenario:

I want to check for particular words, and if they match a term, I will have to update the content of that page and link it to the term. But for now I am focusing on getting the content pages which have a part of the content the same as a particular term.

This is an idea of what I need to do, but it is not working since the subquery returns more than one field.

I want to find WHERE m.module_content is LIKE any of the terms I have, but it should check with them all.

SELECT m.module_termid, t.term_name, m.module_name, m.module_content
FROM modules m
JOIN terms t ON m.module_termid = t.term_id
WHERE m.module_content LIKE  '%' || (SELECT term_name FROM terms) ||  '%'

module_content has text in html format, so eventually all I would need to do is, if it matches a term and it is not yet links, I will add a link to that particular term.

What is the best option to do here? (I am using mysql btw)

To give you an example of what the expected result is:

Terms: id: 1, name: hello Modules: id: 1, content: < p > Hello World < /p >

I would like that modules with id 1 is brought up, since it contains content which somewhere has the term name "hello"

Updated:

Tried Pablo's solution but this is what happens:

enter image description here

"Ray Davis" has nothing to do with the term "Float" for example, so that should not have appeared.

like image 863
Ryan S Avatar asked Nov 04 '11 10:11

Ryan S


People also ask

Can we use like with WHERE in SQL?

The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can we compare two columns in WHERE clause SQL?

Answer. Yes, within a WHERE clause you can compare the values of two columns. When comparing two columns in a WHERE clause, for each row in the database, it will check the value of each column and compare them.

Can you combine like and in statements SQL?

Kevin is right, you cannot combine the in and like items as you've done it. Full text might help, but you'll still be building a string with multiple and statements for your CONTAINS (or other predicate) statement.

How do I create a multiple like condition in SQL?

Description. The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.


2 Answers

I think you just need to change your JOIN condition to something like:

SELECT m.module_termid, t.term_name, m.module_name, m.module_content
  FROM modules m
  JOIN terms t ON (m.module_content LIKE  '%' || t.term_name ||  '%')

Having said that, this could be potentially very inefficient. Consider using a FULL TEXT INDEX INSTEAD for this operation.

like image 171
Pablo Santa Cruz Avatar answered Sep 28 '22 01:09

Pablo Santa Cruz


After a bit of research, my solution would look like this:

SELECT m.module_termid, t.term_name, m.module_name, m.module_content
  FROM modules m
  INNER JOIN terms t ON m.module_termid = t.term_id
  WHERE m.module_content LIKE CONCAT('%', TRIM(t.term_name), '%')

edit: Regarding Paul Morgans comment, I replaced CONCAT('%', t.term_name, '%') with CONCAT('%', TRIM(t.term_name), '%') so that all the whitespaces in t.term_name are stripped off. If you need the whitespaces in t.term_name, just remove the TRIM call and use the old version (CONCAT('%', t.term_name, '%'))

like image 20
hlt Avatar answered Sep 28 '22 01:09

hlt