MySQL like reversed - mysql

I'm trying to get around pulling all the data from a table, and cycling through it with php. Here's my current Query:
SELECT
*
FROM
ExampleTable
WHERE
StringContains LIKE "%lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh%"
ExampleTable.StringContains has values that look like 'someuser#example.com', 'someuser2#example.com', etc.
This doesn't match because LIKE only finds sub strings of the column value, not the other way around. Any ideas on commands to find rows where the table value is a substring of the passed string?

SELECT
*
FROM
ExampleTable
WHERE
'lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh' LIKE
CONCAT('%', StringContains, '%')

Try this:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE
CONCAT("%",StringContains,"%")
The key is to recognize that the column variable just represents a string, and the LIKE statement is always comparing two strings in the form
"stringA" LIKE '%stringB%'
Usually people use it to search for a "part" of a string contained in the "whole" database field string, but you can easily switch them. The only extra tool you need is the CONCAT statement, since you want the database field to be the part instead of the whole. The CONCAT statement builds a string with the %'s around the database field string, and the string form of the argument is now equivalent to
"stringB" LIKE "%stringA%"

Just make the LIKE in the opposit order. Since you have to add those % you'll have to concatenate the field first:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE CONCAT('%', StringContains, '%');

Related

Using a MySQL Select with a RegEx Expression embedded within a String

I am using PHP to access a mysql database field that contains up to 2500 characters per record.
I want to build queries that will return only the records that include a single word, like 'taco'.
Sometimes, however, the user will need to search for a word like 'jalapeno'. Except that jalapeno may exist in the database as 'jalapeno' or as 'jalapeño'. The query should return both instances.
As a further complication, the user may also need to search for a word like 'creme', which may appear as 'creme' or 'créme', but never as 'crémé'.
It seems like I should be able to construct something that uses a replace, and then a Regular Expression, so that the letter 'n' is always replaced with '[n|ñ]', and then search for a string with an embedded Regular Expression like this: 'jalape[n|ñ]o'. Except that does not work. MySQL treats the RegEx syntax as literals.
None of the following return the results that I am looking for:
SELECT id, record FROM table WHERE record like '%jalapeno%';
SELECT id, record FROM table WHERE record REGEXP 'jalapeno';
SELECT id, record FROM table WHERE record REGEXP 'jalape[n|ñ]o';
SELECT id, record FROM table WHERE REGEXP_LIKE(record, 'jalape[n|ñ]o', 'im');
Additionally, I can use PHP to do a replacement of the potential characters, but I end up with stuff like this:
SELECT id, record FROM table WHERE (record like '%creme%' || record like '%crémé%');
I would be Ok with a search like this, but it seems overly complicated to construct programmatically:
SELECT id, record FROM table WHERE (record like '%creme%' || record like '%crémé%' || record like '%cremé%' || record like '%cremé%' );
Is there a MySQL method that provides a REGEX 'OR' to be embedded within a String?
Maybe something like this:
SELECT id, record FROM table WHERE record like '%cr[e|é]m[e|é]%' ;
Or is there another solution that would not require the construction of an excessively convoluted SQL Statement?
Thanks for anyone who spent time trying to figure this out.
As I commented above, REGEXP_LIKE() does not appear to be a valid MySQL function for the current release.
Here is my solution; Note that this works for MySQL 5.7.x.
SELECT id, record FROM table WHERE record RLIKE 'jalape(n|ñ)o';

Searching for entry with id in comma separated list in mysql

I want to get entries from a mysql table, which contain a given id within a comma separated list. I want to use regular expressions and the LIKE selector.
My current approach looks like this
SELECT * FROM table WHERE list LIKE '%,0,%';
with the problem being that this ignores the first and last element in a list like '0,1,2,3'.
I've tried using the | or operator to test for all possible cases.
SELECT * FROM table WHERE list LIKE '(%,0,%)|(^0,%)';
I've tried this with and without the ^ character and with and without the parenthesis, but in all cases this approach didn't even match the characters in the middle. In fact, the or operator doesn't seem to be working in even the simplest expressions like
SELECT * FROM table WHERE list LIKE '%(1|2)%';
You should fix your data model! DO not store lists of things -- especially numbers -- in a string. SQL has a great data model for storing lists: it is called a table.
If you are stuck with someone else's really, really, really bad choice of dta model, you can work around in. MySQL has a handy function, find_in_set(), that does what you want:
WHERE find_in_set('0', list) > 0
Concatenate commas to the start and the end of the list:
SELECT * FROM table WHERE concat(',', list, ',') LIKE '%,0,%';

MySQL search for keywords containing apostrophe or not

I have a MySQL database containing a list of UK towns and one of which is "Connah's Quay".
I want to be able to return results for "Connah's Quay" whether I have used the apostrophe or not, so "Connah's Quay" and "Connahs Quay" returns "Connah's Quay".
Rather than creating a field containing both versions (one with and another without the apostrophe), is there a SQL query I can run that will return results whether I have used the apostrophe or not?
QUERY:
SELECT * FROM uk_postcodes WHERE postal_town LIKE "connahs%";
Standard approach would be to normalise the data and search on that, so something like:
SELECT * FROM uk_postcodes WHERE REPLACE(postal_town, '''', '') LIKE 'connahs%';
This is a bit horrible to do on the fly (and not index friendly), so you would be better to store on table (also means you can also then cope with "Stow-cum-Quy" vs. "Stow cum Quy", etc.)
You might try this:
SELECT *
FROM uk_postcodes
WHERE REPLACE(postal_town,"'","") LIKE CONCAT(REPLACE("connah's","'",""),"%");
This removes the apostrophes from both the search term and the column value before the comparison.

Match a longer string with a shorter one

I have a form where users can search all the entries in a table and i'm having problems with a specific entry.
I'll give an example for a better understanding:
The user can check 5 boxes: 1, 2, 3, 4, 5 and he checks all the boxes.
If the specific row has the value '1|2|3', my search query won't find it.
Beforehand i get the post variable in php (which is an array) and add '%' before and after every value. In this case: '%1%2%3%4%5%'
My search query:
SELECT id FROM table WHERE string LIKE '%1%2%3%4%5%'
In this case, it won't find the row with the value '1|2|3'.
I guess it's normal this way and it works great the other way around but I want to perform a query that matches parts of the original string. In my case '%1%2%3%4%5%' to match '1|2|3'.
How can i do this?
Thanks in advance for taking the time.
EDIT: Can i do this without multiple AND, OR operators?
just change it to this:
string LIKE '%1%'
OR
string LIKE '%2%'
OR
string LIKE '%3%'
...
this would solve your problem - but you should really think about normalizing your database (in this case, store 5 different flags as 1/0 for the five different settings instead of putting them all together in one field).
Before you find something sexier and more efficient, you can use:
SELECT id FROM table WHERE string LIKE '%1%' OR string LIKE '%2%' OR string LIKE '%3%' OR string LIKE '%4%' OR string LIKE '%5%'
I would say - dont use like, use substring or some similar function.

Issue using LIKE in a SQL query with special characters in it

SELECT id, absolute_tag, tag FROM c7_storage_tags
WHERE users_id = 1
AND LOWER(absolute_tag) LIKE LOWER('##Pictures#zee\'s fav%')
This is not giving me the correct result, even thou i do have a row in my database with the correct string.
The better solution for me would be to figure out a way so that "##Pictures#zee\'s fav" string of mine, could be treated as just a string. And I can perform like on it.
How can I fix it.
Thanks in advance.
Zeeshan
IF you string has a backslash in it, you should escape it twice :
// value absolute_tag == "##Pictures#zee\'s fav"
select * from test where `absolute_tag` like '##Pictures#zee\\\\\'%'
From http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
Your query should look like
SELECT id, absolute_tag, tag FROM c7_storage_tags
WHERE users_id = 1
AND LOWER(absolute_tag) LIKE LOWER('##Pictures#zee\\\\\'s fav%')
Your original query was looking for a record with absoulte_tags begins with "##Pictures#zee's fav"