Search code examples
mysqlmysql-error-1064

Mysql create table query problems?


I want to create a table, which name something like :

name_thisyear_thisweekofyear, e.g -> name_2009_40

I tried this query:

CREATE TABLE IF NOT EXISTS name_YEAR(NOW())_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, phone_no int, created datetime, deleted datetime)

It doesnt work. Please help

Error Message:

#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 'NOW())_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, userid int,' at line 1


Solution

  • this can't work, because parantheses are not allowed in table names (unless you escape them properly using backticks)

    mysql sees your query as:

    CREATE TABLE IF NOT EXISTS
    name_YEAR(
        NOW()      # this is causing an error already
    )
    /*GARBAGE: */_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, phone_no int, created datetime, deleted datetime)
    

    i don't know if it's possible at all, to create parts of table names dynamically and concatenating the parts together in the end.

    the easy way would be to calculate the name in php and create the query out of php