3

I select something from a column named "9 stars" (without the "").

SELECT `9 stars` FROM `table` WHERE ... 

works fine, but I have to find a solution to do the job combining a variable (i = 9) with the "stars", something like:

i = 9;  
SELECT `i stars` FROM `table` WHERE ...  (which does not work like this, of course).
Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
  • What environment are you in? How is this query getting to MySQL? – miken32 Jan 21 '16 at 17:43
  • Possible duplicate of [Dynamic conversion of string into column name. MySQL](http://stackoverflow.com/questions/13282718/dynamic-conversion-of-string-into-column-name-mysql) – miken32 Jan 21 '16 at 17:51
  • Your question is very unclear on what you have and what you need leading to divergent answers. – El Gucs Jan 21 '16 at 18:08

4 Answers4

4

If you want to do it in pure Sql then For Dynamic Column name you need to use prepare statement

SET @i =9;
SET @table = <tblname>;
SET @query = CONCAT('SELECT ',CONCAT('`',@i,' ', 'start','`'),' FROM ', @table);

PREPARE stmt FROM @query;
EXECUTE stmt;

check this

Dynamic conversion of string into column name. MySQL

Community
  • 1
  • 1
developerCK
  • 4,418
  • 3
  • 16
  • 35
3

If you don't want to mess around with dynamically constructing your query AND know how many "i" values there are, you could just do this:

SET @i = whatever;
SELECT CASE @i 
   WHEN 1 THEN `1 stars`
   WHEN 2 THEN `2 stars`
   WHEN 3 THEN `3 stars`
   WHEN 4 THEN `4 stars`
   WHEN 5 THEN `5 stars`
   WHEN 6 THEN `6 stars`
   etc....
   ELSE NULL
END AS `the_stars`
FROM table
WHERE ...

You could also use the value from another field of the row instead of a session variable (@i)

Uueerdo
  • 15,723
  • 1
  • 16
  • 21
  • I was going to write this one too, but eating my fruits now :) To be clear because the format doesnt show it. `1 star` is a field name and will return that content – Juan Carlos Oropeza Jan 21 '16 at 18:05
  • Question didn't make it clear how consistent the field naming was, so I went with uniformity for the sake of the example. – Uueerdo Jan 21 '16 at 18:07
  • @Uueerdo, in this particular case, Your approach seems to be Best.out of the box solution, Great Man! (We should make thing simpler not Complex!) – developerCK Jan 21 '16 at 18:11
  • btw I think you mean `user variable` not `session variable` – Juan Carlos Oropeza Jan 21 '16 at 18:15
  • This would be good for a limited number of columns, but could get messy real fast if there were more than 5 or 6. – miken32 Jan 21 '16 at 18:24
  • @JuanCarlosOropeza I am fairly certain I mean `session`. I believe all single `@` variables are connection(session) specific; I think it is possible to have `global` user variables with `@@`, but am not positive since I have little use for them. – Uueerdo Jan 21 '16 at 18:50
  • @miken32 agreed on the feasibility of this kind of solution decreasing with the number of fields chosen in this way; in extreme scenarios the query could actually exceed max length allowed. With fewer fields, this format can be clearer, especially if using another field as the case subject instead of a variable (dynamic sql would involve executing separate queries for each value, or potentially massive unions of such queries.) – Uueerdo Jan 21 '16 at 18:51
  • Great, I got it solved with the CASE construction. Maybe not the most universal way for all needs in the future but in my particular case just perfect. Thanks a lot! – user5564160 Jan 22 '16 at 08:30
1

Use CONCAT

SET @i = 9;
SELECT CONCAT(@i,  'stars') 
FROM table WHERE ... 
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
0

You could use the ELT function in MySQL :

SET @i = 3;
SELECT ELT(@i, `1 stars`, `2 stars`, `3 stars`, `4 stars`[, ...]) FROM `table` WHERE [...];
rhessus
  • 16
  • 2