0

I can't use 'time' word as my column name in mysql query.
This is my query code:(Not Working)

UPDATE ip_p SET deger =  '-1' TIME =  '123' WHERE ip =  '12' AND premium =  '1'

Error is:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'time='123' WHERE ip = '12' AND premium = '1'' at line 1

But this code is working:

UPDATE ip_p SET deger =  '-1' WHERE ip =  '12' AND premium =  '1'

Will I have to change my column name?

Joseph
  • 271
  • 1
  • 2
  • 9

4 Answers4

3

1) You're missing coma (,) after deger = '-1'

2) You may enclose TIME into backticks (`) to prevent other possible errors

Correct (try it?) query spelling:

UPDATE ip_p SET deger =  '-1', `TIME` =  '123' WHERE ip =  '12' AND premium =  '1'

UPDATE

Responding to comment:

correct query for decreasing deger column by 1 will be:

UPDATE ip_p SET deger =  deger-1, `TIME` =  '123' WHERE ip =  '12' AND premium =  '1'

If you just need to set it to -1 then do:

UPDATE ip_p SET deger = -1, `TIME` =  '123' WHERE ip =  '12' AND premium =  '1'

Also please note that syntax with single quotes (') should be used only with strings (varachar(...) columns etc.); for numbers that is incorrect. If you need to specify the number - just do it without any quotes. I've changed this for deger column (apparently it's numeric if you want to subtract something from it) but not for the others because I don't know their types. If columns TIME, ip and premium are also numbers then you'll need to also remove quotes from their values.

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • very thanks.. and i want to change deger column as -1 but this code does operation. if deger column is 1, this query change as 0? – Joseph Jan 22 '12 at 23:38
  • No. The query you have will change the column `deger` to strict -1. I'll update my answer in a moment. – Sergey Kudriavtsev Jan 22 '12 at 23:47
1

You can escape the keyword "time" by using backticks:

UPDATE ip_p SET deger =  '-1', `TIME` =  '123' WHERE ip =  '12' AND premium =  '1'

I believe you also dropped a comma, which may be the cause of the error you are getting here. But in general, if you have a column name which is special to MySQL, escape it.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • very thanks.. and i want to change deger column as -1 but this code does operation. if deger column is 1, this query change as 0? – Joseph Jan 22 '12 at 23:41
  • @Joseph: try `SET deger=-1`, since what you've got will set it to a string, not an integer. – Borealid Jan 22 '12 at 23:42
0

You can, but you must enclose the name in backticks `. As a general rules I always enclose column names in them, as it makes it clear what they are.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

You're missing a comma before TIME:

UPDATE ip_p SET deger =  '-1', TIME =  '123' WHERE ip =  '12' AND premium =  '1'
                             ^
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521