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.
Related
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.
I have created a subquery that searches for a particular string from one table, using the SQL LIKE condition. I would like to use this subquery's result as the string to search for in my main SQL query also using the LIKE condition. I tried the below code but I get syntax errors, although it seems to be the way it should be done...sadly I am not an SQL expert and just trying to feel this out.
SELECT * FROM `allcesseries`
WHERE series_id LIKE '%'+(SELECT industry_code FROM `ceindustry` WHERE industry_name LIKE '%Technical and trade schools%')+'%'
SELECT * FROM `allcesseries`
WHERE series_id LIKE concat('%',
(SELECT industry_code FROM `ceindustry`
WHERE industry_name LIKE '%Technical and trade schools%'),
'%')
I would suggest that you use exists in this case:
SELECT *
FROM `allcesseries` a
WHERE EXISTS (SELECT 1
FROM `ceindustry` c
WHERE c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
);
If you have multiple matches, then this will work as expected.
You can also phrase this directly as a join, if you want:
SELECT a.*
FROM `allcesseries` a JOIN
ceindustry c
ON c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
But if there are multiple rows that satisfy the conditions in ceindustry, you will get duplicates.
I have a field ('roles') with this values
roles
row_1: 1,2,3,5
row_2: 2,13
I do this:
SELECT * FROM bloques WHERE 2 IN (roles)
and only find row_2, because it starts by 2
The LIKE option doesn't work because if I find 1
SELECT * FROM bloques WHERE roles LIKE '%1%'
, it gives me both rows.
FIND_IN_SET function cal helps you:
SELECT FIND_IN_SET('b','a,b,c,d');
For your code:
SELECT * FROM bloques WHERE FIND_IN_SET(2,roles);
Also, I suggesto to you that move your schema to 1NF
Could you use REGEXP? http://dev.mysql.com/doc/refman/5.1/en/regexp.html
Otherwise, you could add commas to the front and end of your rows.
select * from mytable where ',' + roles + ',' like '%,2,%'
SELECT
*
FROM
bloques
WHERE
roles LIKE '%,2,%'
OR roles LIKE '2,%'
OR roles LIKE '%,2'
The first case will give you all cases where 2 is in the middle of a set, the second case is when 2 starts the set, and the third case is when 2 ends the set. This is probably terribly inefficient, but it should work.
You can use this regex here:
SELECT * FROM bloques
WHERE roles REGEXP '[[:<:]]2[[:>:]]';
... or, in more generic case:
SELECT * FROM bloques
WHERE roles REGEXP CONCAT('[[:<:]]', :your_value, '[[:>:]]');
You need to use these weird-looking things, as they match word boundaries - preventing match for '2' to occur at '23' and '32'. )
The problem is that query is only the beginning of the troubles caused by using denormalized field. I'd suggest using either SET type (if the number of options is limited and low) or, way better, junction table to store the m-n relationships.
Use FIND_IN_SET() function
Try this:
SELECT * FROM bloques WHERE FIND_IN_SET(1, roles);
OR
SELECT * FROM bloques
WHERE CONCAT(',', roles, ',') LIKE '%,1,%';
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
...
SELECT *
FROM company_address
WHERE address_telephone LIKE '%12%'
AND address_telephone LIKE '%45%' (edit)
Short question: Is there a way to only use the column name once?
(edit: It was an example, sorry for that.. )
If you really want to do it in one search, you can put your search criteria into a table variable and do a wildcard join on that:
DECLARE #t table (s varchar(20))
insert into #t values('12')
insert into #t values('45')
select ca.* from company_address ca inner join
#t t on ca.address_telephone like '%' + t.s + '%'
Your clause is faulty. address_telephone cannot be LIKE '%12%' without also being LIKE '%125%' so only the second of them is necessary anyway.
If this is only an example case and you didn't actually intend that WHERE logic, REGEXP might work for your situation:
WHERE address_telephone REGEXP '^.*125[0-9]+$'
Short answer: No
Side note: I do not know your business rules for the query, but it looks like you might want to be using OR instead of AND.
Long answer: If it was an equality, you could use IN. However, since you are using LIKE, you have to specify each LIKE condition.
in your example, yes:
SELECT *
FROM company_address
WHERE address_telephone LIKE '%125%'
explanation
if address_telephone LIKE '%125%' is true
then address_telephone LIKE '%12%' must be true as well, so there is no need to add it to the query
You can normally use wildcards etc to combine multiple likes into one statement. See below for a trivial example
declare #test varchar(50)
set #test = '123456789'
select 'true' where #test like '123%456%9'
Edit: That returns 'true', by the way
Maybe you're looking for a Levenshtein distance function? It should allow you to do a fuzzy search. There's one here.