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
Related
I have a column which stores the data from the below format.
["12973","111","5555"].
I want to select the items which is not in the above set.
for ex,
If I search 12973 if it is exactly match it will not return.
When I use Like if I search "129" it also return the same result.
Any ideas to solve this problem..
If you are searching for an exact number you can use NOT LIKE to exclude the row
SELECT * FROM test WHERE col1 NOT LIKE '%"12973"%'
if you are searching for a row where no numbers start with 129 you can use REGEXP
SELECT * FROM test WHERE NOT col1 REGEXP '.*"129[0-9]*"'
What about search all results with LIKE and remove the exact matches?
SELECT * FROM t WHERE a LIKE '%129%' AND a != '129'
So I have an database where I have a string in each row eg: 1,4,56,3,23
I want to find every row where say 1 is in it, so this row would be found a 1 is at the start but if I have 4,1,54,32,2 it wont find it.
This is the code I'm using:
WHERE ".$id." IN ( outcomes )
You can use LIKE because you have a string separated by comma.
SELECT * FROM users WHERE outcome LIKE '41,%' OR outcome LIKE '%,41,%' OR outcome LIKE '%,41';
The above code select users which have outcome 41 in the list.
Use FIND_IN_SET :
SELECT *
FROM table
where FIND_IN_SET(".$id.",outcomes);
You can use the INSTR function as well :
SELECT *
FROM table
WHERE INSTR(".$id.",'1') > 0
In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.
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*%'
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%"