lior r
lior r

Reputation: 2290

MySQLquery database against a string

I'm interested in searching for the database entries against a given string.

Something like this :

SELECT * FROM TABLE WHERE %table_column% LIKE %STRING%

is it possible to do this with MYSQL?

Thanks

Upvotes: 1

Views: 52

Answers (2)

Omesh
Omesh

Reputation: 29091

You can use dynamic MySQL mostly inside stored procedure to accomplish this:

CREATE PROCEDURE sp_test
(
 arg_column_name VARCHAR(255)
,arg_filter VARCHAR(255)
)
BEGIN

SET @query1 = CONCAT('
        SELECT  *
        FROM    table_name a
        WHERE   a.',arg_column_name,' LIKE "%',arg_filter,'%"'
        );
        PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;
END;

CALL sp_test('column_name','filter_val');

Upvotes: 1

juergen d
juergen d

Reputation: 204766

No, you can't wildcard the column names. You can specify them like this

SELECT * FROM TABLE 
WHERE col1 LIKE %STRING%
or col2 LIKE %STRING%
or col3 LIKE %STRING%

Upvotes: 1

Related Questions