1
 SELECT * FROM events WHERE repeat IS NOT NULL

Hello! I have the error when do this select:

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 'IS NOT NULL' at line 2

But It is ok if I do the same select with user_id for example:

SELECT * FROM events WHERE user_id IS NOT NULL

I am wondering, because name of the column is correct

Structure of my table:

  create_table "events", :force => true do |t|
    t.string   "title"
    t.date     "shedule"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "repeat"
  end
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
Gabi
  • 250
  • 1
  • 4
  • 13

1 Answers1

2

Because REPEAT is a reserved keyword. There are two ways to escape from it,

one is by wrapping with backticks,

SELECT * FROM events WHERE `repeat` IS NOT NULL

the second is by using the alias defined on the table

SELECT * FROM events e WHERE e.repeat IS NOT NULL
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • There is a third way to escape it: the standard ANSI SQL way using double quotes: `"repeat"` (which is how it is done in all other DBMS) –  Apr 07 '13 at 17:28