Chris Nelson
Chris Nelson

Reputation: 173

User defined variables in SQL query

I am having a wee bit of trouble calling a variable in phpmyadmin using the SQL query window.

I am still just learning the ropes so this is very simple. I believe it's just a small syntax or quote issue.

What I am trying to do is:

SET @var1 = Value (Does it need quote marks after "=" or not?)

SELECT * From `Table` WHERE 'Column' = @var1      (Same question about quote marks)

This seems just stupid simple. I did a search and just couldn't quite find what I am doing wrong.

Upvotes: 4

Views: 5540

Answers (2)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

You dont need quotes:

SET @var1 =10;
SELECT * FROM table WHERE `column` = @var1 //should work

Or you could do:

SET @var1:='somename';
SELECT * FROM table WHERE `somefield`=@var1

See: variables

Upvotes: 4

bfavaretto
bfavaretto

Reputation: 71939

If your value contains a string, you have to use quotes around it, otherwise you don't. But you should not quote your column name! So:

SET @var1 = 'stringval';
SELECT * From Table WHERE Column = @var1;

Upvotes: 1

Related Questions