I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
Related
I'm having issue retrieving info from a DB by using this query with the lower() function:
SELECT DISTINCT "column_name" FROM "table" WHERE lower('car', 'house', 'plane'...) like '%owner%'
The query works with 1 attribute like for exmaple 'car' but when I try to use 100 I get the following error:
Error Code: 1582. Incorrect parameter count in the call to native function 'lower'
What should I change in order to be able to use and output more than a single attribute?
Thanks.
I think the query you want is something like this:
SELECT DISTINCT column_name
FROM yourTable
WHERE owner REGEXP 'car|house|plane'; -- and maybe other terms in the alternation
That is, you want to match all records where the owner column contains one of the substrings car, house, or plane.
I have value in my db
like
1,12,13,25,44,414,2114
I have to find exact 14 from the db.
but it also return the value 414 and 2114
but i want exact 14.
How can i achieve this through sql query Please help
i have tried this but didn't worked.
Select * from tb_name where columnNAme like '%value%'
If you want search exactly 14, then you need search %,14,%, but 14 may appears at start or at end of string, so you need add commas to column also.
Well, you can use:
select * from tb_name where concat(',',columnNAme ,',') like '%,14,%'
Side note, comma separated values in one column is bad database design
Select * from tb_name where columnNAme like 'value'
% - The percent sign represents zero, one, or multiple characters
SELET * FROM database WHERE column LIKE 14 should work fine, just double checked it on my database.
If this doesn't work can you show your query please?
Above answer is correct, I didn't see your query before I posted this.
I am looking to build a query which can find values in string. For eg my column demo has following 2 rows:
1,2,121,43
343,21
My current query is:
select * from table where demo like '%21%'
However, this returns both the rows. I want to look for the exact match in the string.
Use FIND_IN_SET()
select * from table
where find_in_set(21, demo) > 0
But actually you should really change your table structure. Never store multiple values in one column!
I have in table column pnum_s
I need get only that rows, which value in column pnum_s is exactly 10 symbol and all these symbols are only digits
what query must write for this?
I am trying
SELECT * FROM mytable WHERE pnum_s REGEXP '^\d+$'
But this not returns 0 rows
The pattern you are looking for is either '^[0-9]{10}$' or '^[[:digit:]]{10}$'.
Everything is in the manual.
I think with Mysql you'll want something like this:
^[[:digit:]]{10}$
Check out the reference page.
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
What you're looking for is this:
SELECT * FROM mytable WHERE pnum_s REGEXP '^[[:digit:]]{10}$';
try this pattern:
'^[0-9]{10}$'
I have a table in which the following query works fine:
select *
from session_actions
where action_type IN ('login_failed','channel_recorded')
Now I'm looking forward to some thing like the following:
select *
from session_actions
where action_type IN ('login_failed,channel_recorded')
As you see I want to give the IN operator one single comma separated parameter, but it is not possible
How can I convert this comma separated string into a list of parameters which is acceptable to IN operator?
You might be looking for FIND_IN_SET() function.
select *
from session_actions
where find_in_set(`action_type`,'login_failed,channel_recorded');
SAMPLE FIDDLE
I tried FIND_IN_SET but it was super slow. I rather use haystack REGEXP CONCAT('[[:<:]]', needle, '[[:>:]]') since then.