User Variables in SQL Query

I have a little problem calling a variable in phpmyadmin using the SQL query window.

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

I am trying to do the following:

SET @var1 = Value (Does it need quote marks after "=" or not?) SELECT * From `Table` WHERE 'Column' = @var1 (Same question about quote marks) 

It seems just plain stupid. I did a search and simply could not find what I was doing wrong.

+4
source share
2 answers

If your value contains a string, you need to use quotation marks around it, otherwise you won’t. But you should not indicate the name of your column! So:

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

You do not 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

+4
source

Source: https://habr.com/ru/post/1412216/


All Articles