Check if value exists in a comma separated list [duplicate] - mysql

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

Related

Mysql IN clause with multiple values in string field [duplicate]

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

How to search for rows that are a substring of a value in MySQL? [duplicate]

This question already has answers here:
MySQL SELECT query string matching
(3 answers)
Closed 3 years ago.
I have a full length item code that I am looking for, '34FDE353UE2' and a table full of a shortened version of said item codes, like this
itemCode
============
34FDE
35DCF
34FPE
....
How can I look through my rows to see if any of them match the '34FDE353UE2'? In my example, I would hope to get back the first row since 34FDE is a substring of '34FDE353UE2'. How can I write a query to do this?
Using MySQL 5.6.
With like:
select * from tablename
where '34FDE353UE2' like concat(itemCode, '%')
this will return rows where itemCode's value is the starting chars of '34FDE353UE2'.
If you want rows where itemcode is a substring of '34FDE353UE2' then:
select * from tablename
where '34FDE353UE2' like concat('%', itemCode, '%')
this will work:
select * from tablename where REGEXP_SUBSTR(itemCode,'34FDE353UE2')

IN query is not working group_concat in mysql subquery [duplicate]

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
IN query is not working in sub_query of mysql
Please take look-:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE id IN ( urd.interested_course )) as interested_course_text,
urd.*
from user_registration_data as urd
Here urd.interested_course have values like 1,2,3,4 for some users
user_registration_data table is like
id interested_course
1 1,2,3,4,5
2 1,4,5
course_intersted_in_list table is like
id interested_course
1 mbbs
2 dental
3 basic
Since the interested_course has comma separated ids, you can use find_in_set to search a value in csv string:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE find_in_set(id, urd.interested_course)
) as interested_course_text,
urd.*
from user_registration_data urd;
Also, I recommend staying away from CSV data in RDBMS and normalize the data. that way you'll get a system that scales.
You can use MySQL FIND_IN_SET instead:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE find_in_set(id, urd.interested_course)) as interested_course_text,
urd.*
from user_registration_data as urd
IN() doesn't work on table columns, only against a list inside ()

How do I find the records containing a comma-separated value? [duplicate]

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

MySQL return only the rows which are numeric [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I check to see if a value is an integer in MySQL?
I have been been trying to select only the numeric rows from a field in mysql.
A sample of the data in the database would be:
'1234543'
'12f4231'
'fdfrgr34'
etc.
I want my select statement to only return the numeric rows.
So in this case the only row I would get back would be the '1234543' row.
Any help would be greatly appreciated.
Thanks,
Stefan.
Try this
SELECT * FROM table_name WHERE column_name REGEXP '^[0-9]+$';
You could use a regex, for positive numbers:
SELECT * FROM my_table WHERE my_column REGEXP '^[[:digit:]]+$'
Some adjustments are needed if you want to allow leading/trailing whitespace though.