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
  • 28,754
  • 17
  • 99
  • 133
Gabi
  • 240
  • 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
  • 249,283
  • 65
  • 481
  • 481