How to JOIN table ON .. LIKE ... 'table.variable' - mysql

I have two tables and I am trying to JOIN them and use the LIKE function on mySQL.
My initial code was :
select A.column
from A
join B
on (B.column) like ('%A.column%');
then I searched stackoverflow past answers and I found that my version was not wrong and at the same time I found this version which did not work either:
select A.column
from A
join B
on B.column like '%'+ A.column +'%';
In the end I used this one :
select A.CPM , B.column , A.column
from A
join B
on B.column like concat('%', A.column,'%');
and it worked. My question is why didn't the previous versions work? Is it because of mySQL version? From my research the syntax is correct in all 3 versions. The results on mySQL for the first two were blank though.

First: Won't work
select A.column
from A
join B
on (B.column) like ('%A.column%');
Reason:
This is just string literal, hardcoded value
B.column LIKE '%A.column%'
Second: Won't work
select A.column
from A
join B
on B.column like '%'+ A.column +'%';
Reason:
+ is not for string concatenation but for addition. Example:
SELECT '%' + 'a' + '%' -- result 0, mysql tries to convert to numbers and compute sum.
Third: Will work
select A.CPM , B.column , A.column
from A
join B
on B.column like concat('%', A.column,'%');
Reason:
You build last value at runtime using correct function CONCAT

In MYSQL Like is a string comparison function.
LIKE operator is used to search for a specified pattern in a column not column matching.
In above two example you are matching with column. In last one you first convert column into string and then apply like.

Related

MYSQL - Select based on a dynamic value

I'm looking for a way to select something in one column based on another column's data. I have 2 tables:
input_table - has a column called "year_of_birth"
result_table - has a column called "notes"
you see, that "notes" column might contain a year in there, something like "1980". I need to be able to select rows where the notes field contains a match to the year_of_birth field. I have my statement written like this so far:
select inp.year_of_birth, res.notes, res.result
from order_input inp join order_result res on inp.order_id = res.order_id
where res.result !='no match'
and res.notes like '%' + inp.year_of_birth + '%'
right now i'm getting an error thta says "conversion failed when converting the varchar value '%' to data type int. I'm not sure what i'm doing wrong because I have a similar statement i'm using that has this type of string in it that works...
You need to use CONCAT instead of +
res.notes like CONCAT('%', inp.year_of_birth, '%')
Try this:
select inp.year_of_birth, res.notes, res.result from order_input as inp join order_resultas res on inp.order_id = res.order_id where res.result <> 'no match' and res.notes like CONCAT('%', inp.year_of_birth, '%')

Joining two tables using the LIKE operator

I'm trying to join two tables based on two values being alike. I have this so far but I'm getting a SQL error as soon as I use the %.
SELECT downloads.d_key, payer_email
FROM paypal_log
INNER JOIN
downloads
ON downloads.d_key LIKE "%" + paypal_log.custom + "%"
The downloads.d_key will be within the paypal_log.custom.
Any help appreciated with this.
Try
SELECT 'one' + 'two' FROM DUAL
and see what you get. You'll want to use
LIKE concat('%', paypal_log.custom, '%')
MySQL concatenation operator is ||. You can also use the CONCAT() function:
SELECT downloads.d_key, payer_email
FROM paypal_log
INNER JOIN
downloads
ON downloads.d_key LIKE '%' || paypal_log.custom || '%' ;
As others pointed, you are probably doing something very, very wrong.

Use '%' with column names in MYSQL select statement?

I have a query that looks at partial matches within two mysql tables to make a join:
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.val LIKE table2.val
This works fine as a join, but... some of the values in table2 are actually substrings of the values in table one—specifically they're urls. So, table1.val might equal http://google.com while table2.val = google.com.
How do I use the '%' operator around the table2 val to make this comparison.
Thanks in advance!
Like this:
... WHERE table1.val LIKE CONCAT('%', table2.val, '%')
Note that this will not perform as well as table1.val = table2.val, as it must now search all rows in table2.
Q: How do I use the '%' operator around the table2 val to make this
comparison.
A: You don't :)
You can specify a column by name (e.g. "mycolumn"), by fully qualified name (e.g. "mytable.myname") or by ordinal (e.g. "1").

How do I search using LIKE and wildcards in mysql?

LEFT JOIN schools ON (bt.MidSchool LIKE schools.Name OR **%schools.Name% LIKE bt.ElmSchool**) WHERE ...
This is the portion of my SELECT that I have problems with.
I would like to find if the string in column schools.Name exist in column bt.ElmSchool
When I add % before and after the column name %schools.Name% I get a syntax error. if I use '%schools.Name%' the query is perform but it's looking for the column name instead of its value. I have tried escaping but didnt work. any idea??
...
LEFT JOIN schools ON bt.MidSchool LIKE schools.Name
OR bt.ElmSchool LIKE '%' + schools.Name + '%'
WHERE
...

SQL WHERE using LIKE and comparing to a field

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:
"Ray Davis" has nothing to do with the term "Float" for example, so that should not have appeared.
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.
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, '%'))
MySQL does not have any concatenation operator, and the query should actually be written as:
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, '%');
But what happened:
m.module_content LIKE '%' || t.term_name || '%'
is actually equivalent to
(m.module_content LIKE '%') || (t.term_name) || ('%')
which is always 1. Thus, you have a Cartesian Product =)
UPD: more as a reference to myself, MySQL does have a concatenation operator ||, but to use it one should set PIPES_AS_CONCAT mode:
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)
You may try this instead:
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 + '%')
Instead of "LIKE", using "IN" should be the solution: something like:-
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 IN (SELECT term_name FROM terms);
Try the below query -
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 != '';
For the above query you will get all rows where term_name columns data is present as a whole word in modules table's module_content column. If you dont want to match only on whole word then in that case u can use MYSQL'S regular expression function in place of LOCATE function.
The documentation for LOCATE function can be found out here
I don't think it is a good way to resolve the problem like this.supports that you have a lot of module items,and the popular word is limit.each time you exec the sql,it needs lots of disk io and may block the online mysql db.
my way is like this:
invert index the module content.
search the popular words with the index.
bind the module id to the key word.
as you can see.it is very efficient and fast.so,the problem is how to make inverted index on the module content.sphinx will do a good job.
hope this will help you:)