MySQL select from two concatenated strings not working

I’m trying to select the data from a table called t32 with the following code:

SELECT   timestamp AS "time",   id FROM CONCAT('t', "32") 

However the following error appears:

#1064 - Fehler in der SQL-Syntax. Bitte die korrekte Syntax im Handbuch nachschlagen bei '("t", "32") LIMIT 0, 25' in Zeile 4 

free translation to english:

#1064 - Error in SQL syntax. Please look up the correct syntax near '("t", "32") LIMIT 0, 25' in line 4 

However if I use

SELECT timestamp AS "time", id FROM t32 

everything works like expected.

about help i would be very happy

Add Comment
1 Answer(s)

You are trying to construct a table name in a SQL query. You cannot do this with a regular SQL query.

One solution is dynamic SQL — that is a prepare and exec approach.

However, this really suggests a problem with your data model. In general, constructing table names should not be necessary. All data about a single entity should be in a single table.

So, the query should look more like:

select t.* from t where <somecol> = 32; 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.