This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
I have a field that contains a string values like this: '1,2,3,4'
I want to bring it if contains the number 2,4. But its not working.
I have try this:
SELECT field IN (2,4)
and this:
select find_in_set('2,4', field)
As find_in_set() will only find one string within another, you will have to use many find_in_set searches. Also this is because if you have 1,2,3,4 but want to find 1 and 3 or 1 and 4 you cannot make a finder string to do that.
So to find 1 and 3 in 1,2,3,4 use
SELECT * FROM <table>
where find_in_set('1', COL)
AND find_in_set('3', COL);
This may help you.
WHERE banana IN ('apple', 'banana', 'coconut')
WHERE 3 IN (2,3,6,8,90)
source : mysql check if numbers are in a comma separated list
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
i have a table with values given below
payment_pattern application
0,0,117,9,5 XXXX0004DqjBQAS
0,30,0,29,16,0 XXX000004E79tQAC
30,30,23,29,22,1 XXX000006F2brQAC
0,0,0,0,29,28 XXXB000006Fs3oQAC
Need to find regular expression that would return all rows with values greater than or equal to 30.
Example output should be
0,0,117,9,5 XXXX0004DqjBQAS
0,30,0,29,16,0 XXXX0004E79tQAC
30,30,23,29,22,1 XXXX0006F2brQAC
You could use REGEXP here:
SELECT payment_pattern, application
FROM yourTable
WHERE payment_pattern REGEXP '[[:<:]]([3-9][0-9]|[1-9][0-9][0-9][0-9]*)[[:>:]]';
But, you would do better to not store unnormalized CSV data in your tables. Your query would be trivial if each number were stored in a separate record.
Here is a demo for the above regex:
Demo
Here is a brief explanation of the above regex:
(
[3-9][0-9] match 30 to 99
[1-9][0-9][0-9][0-9]* match 100 to 999, or this plus any other digit
)
This question already has answers here:
How to combine multiple columns as one and format with custom strings?
(4 answers)
Closed 8 months ago.
I want to convert selected values horizontally into a comma separated string in MySQL.
The query is:
SELECT user_a, user_b, user_c FROM tb_loterijos_laimetojai
My desired output would look like this (in one string):
Admin, Admin, Admin
Admin, Admin, Admin
How can I achieve this?
You can use concat_ws():
select concat_ws(',', user_a, user_b, user_c)
Note that having multiple columns with the same reference is generally an anti-pattern. You should probably have another table with one row per whatever and user.
SELECT CONCAT(user_a, ",", user_b, ",", user_c) FROM tb_loterijos_laimetojai
Use concat() mysql function
SELECT CONCAT(user_a, ",", user_b, ",", user_c) as user FROM tb_loterijos_laimetojai
This question already has answers here:
Find value in comma delimited string mysql
(3 answers)
Closed 6 years ago.
I've got a field in my database which contains comma-separated integers, for example:
"4,6,18,26,29,34"
I need to construct an SQL query which will return the record which contains a specific given integer, for example 6, so my current query is like this:
SELECT * FROM mytable WHERE CSVField LIKE "%,6,%"
I've surrounded the desired value with commas to avoid 6 matching 26 however it's obvious that my current query won't match against the first or last values in the field because the field doesn't start or end with a comma, so in the example above it'll never find 4 or 34.
How can I write my query so that it'll do what I want?
You can try:
SELECT * FROM mytable WHERE CSVField LIKE "%,6,%"
OR CSVField LIKE "6,%"
OR CSVField LIKE "%,6"
Hope you table is quite small though.
UPDATE
Following Mihai's post, try:
SELECT * FROM mytable WHERE FIND_IN_SET(CSVField,'6') > 0
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
I have following values in my sql column:-
a,b,c,d e,f
I want to check if b is present in the column.
You can use FIND_IN_SET():
FIND_IN_SET('b',yourcolumn) > 0
As an example to be used in a query:
SELECT * FROM yourtable WHERE FIND_IN_SET('b',yourcolumn) > 0;
you can use FIND_IN_SET()
FIND_IN_SET('a','a,b,c,d,e');
http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
You can use a like clause on the column, for eg, if the column name contains these values in the users table you can use this query
select * from users where name like "%b%";
Try this:
select * from users where name like "%,b,%" OR name like "b,%" OR name like "%,b" ;
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
I work with a system that used comma separated values as a one-2-many relation. It is stored in as a blob.
I try to use the MySQL IN (LIST), but only ends up with the rows where the uid is first in the list or only one in the list.
mytable
uid categories
1 24,25,26
2 25
3 22,23,25
4 25,27
run sql:
SELECT * FROM mytable WHERE 25 IN (categories)
Result:
uid categories
2 25
4 25,27
Missing (should have been selected but are not)
uid categories
1 24,25,26
3 22,23,25
Any idea why a string has to start with 25 and not just contain 25? I guess it is a string issue rather than an IN (LIST) issue
UPDATE - based on answers below:
When using th IN (LIST) on a blob, the blob is converted to an int and commas and "digits" are lost.
In stead use FIND_IN_SET(needle, haystack) that will work also on a blob containing comma separated values.
I think you are looking for FIND_IN_SET
SELECT * FROM mytable WHERE FIND_IN_SET(25,category)
This should give you your asnwer:
SELECT * FROM mytable WHERE CONCAT(',', categories, ',') LIKE '%,25,%'
But it would make more sense to create a connecting table, with each comma separated value as a new row.
What's happening is that MySQL is converting the blob to an integer (not a list of integers) for comparison. So '24,25,26' becomes the value 24. You can confirm this by running the query
SELECT CAST(categories AS signed) FROM mytable
This is not how IN (nor the blob data type) is meant to be used. Is there a reason you're not using another table for this?
Yes, it's astring issue. Your 'list' is just a string to mysql, meaning nothing.
You would have to use RegEx.
But you might think about normalizing your tables instead.