My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FĂ©lixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'
Related
I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it
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 have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
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 use a string for store the days of the week, something like this:
MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).
I don't know how to do this in SQL (MySQL).
use LIKE with %-wildcard between the single characters:
SELECT * FROM table WHERE column LIKE '%M%F%';
note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).
if the characters can be in random order, you'll have to built a query like:
SELECT * FROM table WHERE
column LIKE '%M%'
AND
column LIKE '%F%'
[more ANDs per character];
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'
Learn more:
http://www.sqllike.com/
Can you not just say
SELECT * FROM blah WHERE weekday LIKE "%MF%"