So I have a list of MerchantNames like so, stored in the Merchant table:
MerchantName
------------
Al's
Bart's
Mo's Cafe
And I want to do a query like so:
select merchantDetail
from merchant
where merchantDetail like '%' + merchantName + '%'
I already tried the above but (as i suspected) it complained about an error in MySQL syntax. I want to do this for every merchant Name in the column. Is something like this possible? Thank you very much.
The correct syntax for the WHERE clause in MySQL would be:
where merchantDetail like concat('%', merchantName, '%')
The + is SQL Server syntax for string concatenation. Note that because you are using LIKE, the '%' and '_' characters have special meanings. This could affect any merchant names with those characters.
Related
I'm trying to select all values in a table based off the userinput. My issue is when I try to write the query and include the '%' character, I always run into errors. I've tried at least 10 different placements, but I always receive an error. The database being used is MySQL.
SELECT * FROM clients WHERE UserName = %:UserName;
If you want to use the operator LIKE with the wildcard '%' you must concatenate it to the user's input:
SELECT *
FROM clients
WHERE UserName LIKE CONCAT('%', :UserName);
If you want all rows with UserName that contain :UserName:
WHERE UserName LIKE CONCAT('%', :UserName, '%');
Are there any differences between the following two queries:
SELECT name from Roster WHERE id LIKE %
and:
SELECT name from Roster WHERE id LIKE %%?
They are equivalent. The % wildcard matches zero or more characters. So both of these check that id is not NULL.
Note that the like pattern needs to be a string. So, both your example would generate syntax errors. You want:
where id like '%'
where id like '%%'
Also, if id is a number, then you should not be using a string operation on it -- under most circumstances.
Here you will get clarification :
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, '%')
If someone passes a '%' to a field that compares in my sql with su.username LIKE CONCAT('%', email ,'%')) it returns all rows. It ends up looking like su.username LIKE CONCAT('%%%'). Can I get around this in anyway without filtering out the '%'?
I'm assuming you mean you want to escape the % so it matches a literal % instead of anything.
In that case, you just need:
... su.username LIKE CONCAT('%',REPLACE(email,'%','\\%'),'%')
You need to escape the %, so it literally matches '%'
select * from mytable
where mycol like '%\%%';
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
...