0

probably been asked before but I couldn't find an answer.

Is it possible to specify a user defined variable in a mySQL query. Such as:

select * from table where column like "%MYVAR%" or column like "%MYVAR%"

Basically, it would be a bit more complicated than that but if I wanted to use the same text multiple times how could I set this and reuse it?

Many thanks.

1
  • What is meant by user defined variable? Commented Oct 14, 2011 at 10:21

2 Answers 2

5

It is possible -

SET @var1 = 'table%';
SELECT * FROM table1 WHERE column1 LIKE @var1;

More information - User-Defined Variables.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for that. In SQLyog I have to run the SET command first and then, separately, run the query (using 2 different query windows). Can this be done from the same query window? Not essential, but would cut a few clicks out.
You can do it in dbForge Studio for MySQL (free express edition) - devart.com/dbforge/mysql/studio
0

To get this working, you should use stored procedure. Check Documentation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.