I have a table which stores ID's in a comma separated string as follows:
field_id_13
------------
1234,5266,3678,4733,7372,5766,2578
and I'm using the following Active Record Construct (CI), to pull out the data:
$this->db->select("*")
->from("channel_data d")
->join("channel_titles t","t.entry_id=d.entry_id")
->where("d.field_id_13 LIKE '%".$id."%'")
->where("t.status","open")
->get();
The problem is that, sometimes on my search, the ID of '266' will be returned, therefore - because of my % surrounding the clause are returning a result matching against '5266'.
Whats the alternative here to make sure it only returns the correct ID/Rows?
Hopefully this makes sense.
You have to use FIND_IN_SET in where condition
$this->db->where("FIND_IN_SET('$id',d.field_id_13 ) !=", 0);
The simplest way, if not the most elegant, would be to add commas to the start and end of the values in the id field, and then search for the value surrounded by commas as well. In other words:
field_id_13
------------
,1234,5266,3678,4733,7372,5766,2578,
and:
$this->db->select("*")
->from("channel_data d")
->join("channel_titles t","t.entry_id=d.entry_id")
->where("d.field_id_13 LIKE '%,".$id.",%'")
->where("t.status","open")
->get();
SELECT * FROM your_table WHERE field_id_13 REGEXP ('^266,|,266,|266$')
Replace 266 with your target ID.
http://sqlfiddle.com/#!2/e6a74/1
The FIND_IN_SET solution looks more elegant, but this should work, too:
->where("d.field_id_13 REGEXP '[[:<:]]{$id}[[:>:]]'")
Related
I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';
in my table i have a feild user_ids. the user_ids feilds containeing the values like 12,45,78,95,21,78,98
what i need is i need an mysql query that search for a specific id(for ex:45) in the feild.
i used like operator but its not working as i expected. for ex: when i search for 5 its return tru, but the id 5 not in the list.
i would like to know is there any default function is available in mysql.
could you pls help me...
my query.
SELECT * FROM `FRIENDSLIST` WHERE `USERS_ID` LIKE '%'.$ID.'%';
NB: i dont know whether this question meets standard,pls dont do down vote. pls help me....
This also works:
select * from FRIENDSLIST where find_in_set( $ID, USERS_ID ) <> 0
Try
Where ',' + MyField + ',' Like '%,' + MySearchString + ',%'
The problem is that you're thinking of it as IDs, but it's just a string. So when you search for '5' in '12,45,78,95,21,78,98' it finds it in the 5 of the 45. If you surround with commas then it searches for ',45,' in ',12,45,78,95,21,78,98,' and finds it, but is you look for ',5,' it won't match, as desired.
you also need to add commas at the beginning and end to be able to math the first and last IDs.
As per your data simpler way is to search with comma as like ',45,'.
But better way is it split them based on comma and matching to it.
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 ,
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.
I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'