Reputation: 940
Shows all cities containing "a" :
SELECT * FROM user WHERE city LIKE '%a%'; -- works fine
What is the correct syntax :
SET @param = 'city';
SELECT * FROM user WHERE @param LIKE '%a%'; -- NOT yielding any rows
Tested in MySQL Workbench query
Upvotes: 0
Views: 2190
Reputation: 1
you can use
select id,date,@statement as any_name from table_name ;
SET @statement = (SELECT To_days( date_f ) - TO_DAYS( date ) FROM `table_name `);
Upvotes: 0
Reputation: 940
The correct syntax goes like this:
SET @param = 'city';
SET @statement = CONCAT('SELECT * FROM user WHERE ', @param, ' LIKE ', '"%a%"');
PREPARE myquery FROM @statement;
EXECUTE myquery;
DEALLOCATE PREPARE myquery
Things to keep in mind:
... user WHERE city LIKE '%a%';
rather than :
... userWHEREcityLIKE'%a%'
'%a%' should become '"%a%"'
DEALLOCATE in the end - you declared a global var 'myquery' valid by default till the session reset - may cause collisions and errors
Tip To see the result of CONCAT(), use
SELECT @statement
Upvotes: 2