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
...
Related
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.
I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.
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.
I use a string for store the days of the week, something like this:
MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).
I don't know how to do this in SQL (MySQL).
use LIKE with %-wildcard between the single characters:
SELECT * FROM table WHERE column LIKE '%M%F%';
note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).
if the characters can be in random order, you'll have to built a query like:
SELECT * FROM table WHERE
column LIKE '%M%'
AND
column LIKE '%F%'
[more ANDs per character];
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'
Learn more:
http://www.sqllike.com/
Can you not just say
SELECT * FROM blah WHERE weekday LIKE "%MF%"
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.