use of like statement with or in mysql - mysql

I am working on a table with name exp1, in this table there is a column named filename which contain data like exp_ara1.txt, exp_gma1.txt, exp_sly1.txt., exp_rcy1.txt, exp_thy1.txt
By using MySQL query I want to select only those rows which have filename either %ara% or %gma%.
I am using following command:
Select * from exp1 where filename LIKE ('%ara%' OR '%gm%');
but it is results
Your SQL query has been executed successfully.
MySQL returned an empty result set (i.e. zero rows).

Try this:
Select * from exp1 where filename LIKE ('%ara%') OR filename LIKE ('%gm%');

I think this is one of the best process as if you have more multiple words to put in like::
$sql = array('0'); // Stop errors when $words is empty
foreach($words as $word){
$sql[] = 'filename LIKE %'.$word.'%'
}
$sql = 'SELECT * FROM exp1 WHERE '.implode(" OR ", $sql);

Using REGEXP we can write a single term to match using an alternation:
SELECT *
FROM exp1
WHERE filename REGEXP '(.*ara.*)|(.*gm.*)'

In MySQL you don't have the option to use the CONTAINS function like in SQL Server or Oracle, which is what it looks like your trying to do.
SELECT *
FROM exp1
WHERE (filename LIKE ('%ara%') OR filename LIKE ('%gm%'));

Related

Filepath comparsion in MySQL

I am executing a MySQL query
select *
from table
where filepath LIKE '/Videos/ABC-Copy.mp4'
but I am getting zero rows as a result even though an exact same value exists in filepath column...
I have also tried using "//" in the query but am getting zero rows as result.
What am I missing ?
When you are using like, it's common to use the '%' character. Otherwise you don't need like. You can just do an equal.
For example what your above query actually does is.
select * from table where filepath = '/Videos/ABC-Copy.mp4'
What you want is
select * from table where filepath LIKE '%/Videos/ABC-Copy.mp4'
try with % symbol..
select * from table where filepath LIKE '%/Videos/ABC-Copy.mp4'

mysql matching encrypted values

When I run a query like this:
SELECT * FROM table WHERE columnName = AES_ENCRYPT('value','SecretKey')
I'm returned an empty set, even though there are rows in the db that match the search query.
What would the correct syntax for something like this look like?
SELECT * FROM table WHERE columnName = AES_ENCRYPT(columnName,'SecretKey')='value'

mysql like query with special character '

I wrote query for filter data using name and wrote following query
SELECT * FROM (`abc`) WHERE (name LIKE "%test\'!&##$\%-(3)\_er%")
It should return records which has name start with text "test"
but it will not instead of if I modify query like
SELECT * FROM (`abc`) WHERE (name LIKE "%test%\'!&##$\%-(3)\_er%")
then it will give result. Why it is not give result with first query?
Is there any other way to do this?
The % is the wildcard in the query.
So %test means everything that ends with test.
and test% means everything that begins with test.
and %test% means everything with test in it.
Simpy change your query to
SELECT * FROM (abc) WHERE (name LIKE "test%")
If you want records that start with test, simply use
SELECT * FROM (`abc`) WHERE (name LIKE "test%")

Mysql query that replaces a unknown number in a path

Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo

Why does this string comparision mysql query return nothing?

I execute the following:
SELECT url FROM mytable WHERE 1
result: url: 'http://www.ciao.es/Epson_Stylus_S22__2007613'
Everything ok by now...but when I do:
SELECT * FROM mytable WHERE url = 'http://www.ciao.es/Epson_Stylus_S22__2007613'
I get nothing!!!
I tried using LIKE, changing the quotes, etc...
what am I doing wrong?
You are having ' ' in your table field url .So if you are querying you will get output like this:
'http://www.ciao.es/Epson_Stylus_S22__2007613' (with in single quote)
SO
Write in this way:
SELECT * FROM mytable WHERE url = "'http://www.ciao.es/Epson_Stylus_S22__2007613'"
Or remove single quote from table.