4

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.

1
  • If you are using a reserved word and wanted to use it as fieldname, use double quotes(single quotes doesn't work, it's for string), it's ANSI SQL-compliant. And besides, backtick is not stackoverflow-friendly, you'll not be able to use them explaining the codes inside of sentences Commented May 13, 2012 at 5:40

2 Answers 2

4

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

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

2 Comments

Believe you just helped me with two reasons why it wasn't working. I wasn't using a semicolon after the "SET" command. And after reading the link you have I think my value isn't compatible. The value I am trying to use looks like '10-12001'
if thats string then you could use something like in the added answer
1

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;

3 Comments

K good to know. We have now moved past the syntax error and I am getting this: #1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
And I just checked and none of my stuff is using utf8_unicode. It's all utf8_general
Problem solved. I just had to change a couple settings. Thanks guys!

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.