I'm trying to select all rows that contain only specific alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab%'
For instance:
abc1234 is ok
abd1234 is ok
abe1234 is not ok
abg4567 is not ok
ab1234 is ok
ac1234 is not ok
The problem is, it select all the "abe","abg". How to select abc1234, abd1234 and ab1234?
With this
OR column LIKE '%ab%'
As part of the WHERE clause, it's no surprise that abe and abg are selected.
Please permit me to also mention that queries LIKE '%something%' cannot make use of any indexes and are likely to be very slow on large tables. The fact that you have three of them in one query is only going to make it worse.
Use the below Query which will also use indexes on column column
SELECT * FROM table WHERE column LIKE 'ab%' AND column not LIKE 'abe%'
AND column not LIKE 'abg%'
The Reason the indexes will be used in this case is the query is not using wildcard in the beginning of the string literal
Remove the last part of your query, it is telling it to select abg and abe.
You have told it to select all items beginning with anything and ending with anything providing ab is in the middle.
Because column LIKE '%ab%' will get "abe" or "abg"
If you want number after "ab", this is your query:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab0%' OR column LIKE '%ab1%' OR column LIKE '%ab2%' OR column LIKE '%ab3%' OR column LIKE '%ab4%' OR column LIKE '%ab5%' OR column LIKE '%ab6%' OR column LIKE '%ab7%' OR column LIKE '%ab8%' OR column LIKE '%ab9%'
Are you going to select column contain "abc" or "abd" or "ab" but not "abe"or "abg". For this case i don't think you can use LIKE. Try to use REGEXP. i think "ab[^g^e]?" should do the job for you.
SELECT name FROM table WHERE column REGEXP 'ab[^g^e]?';
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
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 database with columns with the same name and a number after it, like:
med1, med2, med3, med4..... etc.
Is it posible to do something like this:
Select medewerker.med*
FROM table
Instead of this?:
Select medewerker.med1,
medewerker.med2,
medewerker.med3,
medewerker.med4
FROM table
Nope. you can use LIKE in a where clause but definitely not when specifying the columns to select
Use this instead if the only fields are the ones specified above.
SELECT *
FROM table
if not you cannot use a LIKE statement on column SELECT's.
if you are refering to values inside the medewerker column then use
SELECT *
FROM table
WHERE medewerker LIKE 'med%'
basically, i need a query that looks mostly like this...
SELECT column FROM tablename WHERE column LIKE '%images/uploads%';
There only catch here is, that I only want a return where anything after uploads is ok as long as it's NOT a forward slash /
So, images/uploads/somethingelse is NOT what i'm looking for
images/uploadssomethingelse IS what i'm looking for.
Any ideas?
Thanks!
This would help you
SELECT column FROM tablename WHERE column LIKE '%images/uploads%' AND column NOT LIKE '%images/uploads/%';`
SELECT column
FROM tablename
WHERE
column LIKE '%images/uploads%'
and column not like '%images/uploads%/%';
SELECT column FROM tablename WHERE column REGEXP 'images/uploads[^/]*$';
I am using mysql database ...
I have a table with col name food_type
in this field all the food types are in comma separated.
Now my problem is that i want to get the search result from them.
For Example:
Data in food_type col is like BBQ,Fast Food,Desi,Seafood,Vegetarian,
And I want to search BBQ,Seafood. But it can't give me the accurate result .
i try to use like in my sql query but same result :-(
How can I achieve this .
You could even use find_in_set() function
select * from table
where find_in_set('bbq',field_name) and find_in_set('Seafood',field_name)
but, as already written, your table needs to be normalized.
You can try 2 LIKE queries:
select * from table where field like "bbq" and field like "seafood"