mysql matching encrypted values - mysql

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'

Related

How to write query to search particular string in a table which has more column fields in sql

I would like to know how to write sql query to search string in all columns in a table.
i.e in single where condition
I have column1, column2,... column50 fields in a table
Right now am using query like
select * from tblist where column1 like '%searchstr%'OR column2 like '%searchstr%' OR ....it goes on
Is there anyway to write sql query to search string in all columns
In the case of your exact query as given, we can try using IN:
SELECT *
FROM tblist
WHERE 'searchstr' IN (column1, column2, ...);
If you really need to use LIKE here, then there is no real shortcut available.
you could save on the grunt work by using the metadata tables to generate your query and then run it. Eg:
select concat('%searchstr% like ',COLUMN_NAME,' OR ')
from information_schema.columns t
where table_name='t' /*change to the table name*/

mysql match special character % from a text

I have to select all rows from a table where column (varchar) values contain '%' in the text.
I tried below query but failed to get correct result set.
SELECT * from TABLE where VALUE LIKE '%%%'
Above query gives all rows of the table.
Please help me to form a query to match '%' and get the correct results.
SELECT * FROM your_table
WHERE value LIKE '%\\%%'
SQLFiddle demo
Escape character worked for me.
SELECT * from TABLE where VALUE LIKE '%\%%'
This query gives me correct result set.

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'

Wildcard for a column name in the WHERE clause of a SELECT statement?

Is it possible to have a wildcard in a column name specified in the WHERE clause? I want to select something but only if a bunch of columns match a certain value (1 in this case). For example:
SELECT COUNT(id) FROM records WHERE *_check = 1
I have a bunch of columns that have _check as the suffix and it would be really nice to use something similar to the above code (which doesn't work).
You could query the information_schema to get the columns in one query
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'foo'
AND table_name = 'bar'
AND column_name LIKE '%_check'
and build your query from the result (pseudo code)
query = "SELECT COUNT(id) FROM records WHERE ";
foreach columName in columnNames
query = query + " " + column_name + " = 1 OR "
next
query = TrimLastOr(query);
But I wouldn't recommend that because mysql information_schema query have a poor performance since they read from disk for every query.
Better: Use a view that returns
SELECT id FROM records WHERE col1_check=1 or col2_check=2 ...
so you can use it in your logic on multiple places, and only have to update the view if you add another _check column.
No.
If you want to do something like this, you need the equivalent of dynamic SQL.

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.