SQL WHERE using LIKE and comparing with a field

I have a scenario like this:

I want to check certain words, and if they match the term, I will have to update the contents of this page and associate it with the term. But now I'm focusing on making content pages that have some content match a specific term.

This is the idea of ​​what I need to do, but it does not work, as more than one field is returned in the subquery.

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

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 html text, so ultimately all I need to do is match the term and not the links yet, I will add a link to this specific term.

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

To give you an example of the expected result:

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

I would like the modules with id 1 to be raised, as it contains content that is somewhere called "hello"

Updated:

Pablo solution fixed, but this is what happens:

enter image description here

Ray Davis has nothing to do with the term Float, for example, so it should not appear.

+5
source share
7 answers

I think you just need to change the condition JOINto 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 ||  '%')

, . FULL TEXT INDEX INSTEAD .

+5

:

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: , CONCAT('%', t.term_name, '%') CONCAT('%', TRIM(t.term_name), '%') , t.term_name . t.term_name, TRIM (CONCAT('%', t.term_name, '%'))

+4

MySQL , :

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

:

m.module_content LIKE  '%' || t.term_name ||  '%'

(m.module_content LIKE  '%') || (t.term_name) ||  ('%')

1. , =)

UPD: , MySQL ||, PIPES_AS_CONCAT:

mysql> SET sql_mode= 'pipes_as_concat';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT 'qwe' || 'asd';
+----------------+
| 'qwe' || 'asd' |
+----------------+
| qweasd         |
+----------------+
1 row in set (0.00 sec)
+2

:

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 +  '%')
+1

"LIKE" "IN" : - : -

SELECT m.module_termid, t.term_name, m.module_name, m.module_content
  FROM m JOIN terms t ON m.module_termid = t.term_id
  WHERE m.module_content IN (SELECT term_name FROM terms);

+1

-

SELECT 
    tp.module_termid, 
    tp.term_name, 
    tp.module_name,
    tp.module_content  
FROM (
        SELECT 
            m.module_termid, 
            t.term_name, 
            m.module_name, 
            m.module_content,
            IF(LOCATE(t.term_name,m.module_content)!=0, m.module_content, ' ') 
                as required_content 
        FROM modules m 
        LEFT JOIN terms t ON m.module_termid = t.term_id
     ) tp 
WHERE tp.required_content != '';

, term_name module_content. , LOCATE MYSQL.

LOCATE

0

, this.supports, , - limit.each, sql, io -mysql db. :

  • .
  • .
  • .

. . , . sphinx . , :)

0

All Articles