MySQL Select Row Where Column Contains a Value - mysql

I have tried using 'LIKE' but it runs into problems which i will explain below.
i have a string column that looks like any of these.
"1010, 2020, 3030"
"1010"
""
I want to be able to see if this string contains a single ID. e.g 2020. if it does then return the row. I tried using like but if the id is 20 it will return the row because 2020 contains 20.
Selecting the entire db and then using a delimiter to go through all the strings will take far too much time. Is it possible to implement this?

This is why you don't store multiple values in a single field. Because your bad design, this is the query structure you'll have to use EVERY SINGLE TIME to compensate for it:
WHERE
foo = 2020 // exact match, only value in field
OR foo LIKE '2020,%' // value is at start of field
OR foo LIKE '%,2020,%' // value is somewhere in the middle of the field
OR foo LIKE '%,2020' // value is at the end of the field
Or you could have had a properly normalized design, and just done
WHERE childtable.foo = 2020
and be done with it.

First, you should not store lists of things in string variables. SQL has a very nice data structure for lists. It is called a table. Each row in such a table would have an id and one value from the list.
That said, sometimes you are stuck with data like this. In that case, you can use find_in-set():
where find_in_set('20', replace(stringcolumn, ', ', ',')) > 0;
You can also do the logic with like, but MySQL has the convenient built-in function for this.
EDIT:
If you want to do this with like:
where concat(',', stringcolumn, ',') like '%,20,%'
Note that the delimiters "protect" the values, so 20 is not confused with 2020.

Related

How do I create a SELECT conditional in MySQL where the conditional is the character length of the LIKE match?

I am working on a search function, where the matches are weighted based on certain conditions. One of the conditions I want to add weight to is matches where the character length of the query string in a LIKE match is longer than 4.
This is what I want to the query to look like, roughly. %s is meant to represent the actual match found by LIKE, but I don't think it does. I'm wondering if there is a special variable in MySQL that does represent the precise character match found by LIKE.
SELECT help.*,
IF(CHAR_LENGTH(%s) > 4, 2, 0) w
FROM help
WHERE (
(title LIKE '%this%' OR title LIKE '%testy%' OR title LIKE '%test%') OR
(content LIKE '%this%' OR content LIKE '%testy%' OR content LIKE '%test%')
) LIMIT 1000
edit: I could in the PHP split the search string array into two arrays based on the character length of the elements, with two separate queries that return different values for 'w', then combine the results, but I'd rather not do that, as it seems to me that would be awkward, messy, and slow.
Check out FULLTEXT as another way to discover rows. It will be faster, but won't address your question.
This probably has the effect you want.
SELECT ....
IF ( (title LIKE '%testy%' OR
content LIKE '%testy%'), 2, 0)
....
Note that the "match" in your LIKEs includes the %, so it is the entire length of the string. I don't think that is what you wanted.
REGEXP "(this|testy|that)" will match either 4 or 5 characters (in this example). It may be possible to do something with REGEXP_REPLACE to replace that with the empty string, then see how much it shrank.
I think the answer to my question is that what I wanted to do isn't possible. There is no special variable in MySQL representing the core character match in a WHERE condtional where LIKE is the operator. The match is the contents of the returned data row.
What I did to reach my objective was took the original dynamic list of search tokens, iterated through that list, and performed a search on each token, with the SQL tailored to the conditions that matched each token.
As I did this I built an array of the search results, using the id for the database row as the index for the array. This allowed me to perform calculations with the array elements, while avoiding duplicates.
I'm not posting the PHP code because the original question was about the SQL.

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';

MySQL Query conditional find nth element in column string

I have a MySQL table setup where one column's values are a string of comma-separated True/False values (1s or 0s). For example, in the column, one field's value may be "0,1,0,0,0,0,1,1,0" and another may be "1,0,0,1,1,1,0,0,0" (note: these are NOT 9 separate columns, but a string in one column). I need to QUERY the MySQL table for elements that are "true"(1) for the "nth element" of that column's value/string.
So, if I was looking for rows, with a specific column, where the 3rd element of the column's value was 1, it would produce a list of results. So, in this case, I would only be searching for "1" in the fth place (12345 = X,X,X...) of the string (X,X,1,X,X,X,X,X,X,X). How can I query this?
This is a crude example of what I am trying to do ...
"SELECT tfcolumn FROM mytable WHERE substr({tfcolumn}, 0, 5)=1"
{tfcolumn} represents the column value
5 represents the 5th position of the string
=1 represents what I need that position to equal to.
Please help. Thanks
You can't. Once you put a serialized data type into a column in SQL (like comma separated lists, or JSON objects) you are preventing yourself from performing any query on the data in those columns. You have to pull the data in a different way and then use a program like python, VB, etc to get the comma separated values you are looking for.
Unless you want to deal with trying to make this mess of a query work...
I would recommend changing your table structure before it's too late. Although it is possible, it is not optimized in a format that a DBMS recognizes. Because of that the DBMS will spend a significant amount of time going through every record to parse the csv values which is something that it was not meant to be doing. Doing the query in SQL will take as much time (if not more time) than just pulling all the records and searching with a tool that can do it properly.
If the column contains values exactly like the ones you posted, then the Nth element is at the 2 * N - 1 position in the comma separated list.
So do this:
SELECT tfcolumn
FROM tablename
WHERE substr(tfcolumn, 2 * 5 - 1, 1) = '1'
Replace 5 with the index that you search for.
See the demo.
Or remove all commas and get the Nth char:
SELECT tfcolumn
FROM tablename
WHERE substr(replace(tfcolumn, ',', ''), 5, 1) = '1'
See the demo.
Try this
if substring_index(substring_index('0,1,0,0,0,0,1,1,0',',',3),',',-1)='1'
The first argument can be your column name. The second argument (',') tells the function that the string is comma-separated. The third argument takes the first 3 elements of the string. So, the output of inner substring_index is '0,1,0'.
The outer substring_index has -1 as the last argment. So, it starts counting in reverse direction & takes only 1 element starting from right.
For example, if the value in a particular row is '2,682,7003,14,185', then the value of substring_index(substring_index('2,682,7003,14,185',',',3),',',-1) is '7003'.

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,%';

SQL - If input string is empty return all rows , otherwise try to match

I have table matches with these columns:
|sport|region|country|league
If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.
If sport is not empty then find rows with matched sports and proceed to region and do the same thing.
Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.
Try this
WHERE (sport_param IS NULL OR sport_column = sport_param)
You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.
Take a look at this
... you can basically make an if statement in SQL to match whether sport is empty or not and depending on that execute two different queries.