Question

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

Was it helpful?

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

OTHER TIPS

You can't do that... You must specify table name, a function can't be executed there

An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. The set of alphanumeric characters from the current character set, “_”, and “$” are not special.

You can't do that. Table and column names must be given as an indentifier.

PS: To concatenate elsewhere, use concat().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top