1

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

1
  • DO you mean you want to search all columns for the same string? Commented Jul 23, 2012 at 10:08

2 Answers 2

1

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%
Sign up to request clarification or add additional context in comments.

Comments

1

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.