Reputation: 463
Just wondering if possible to do this:
I have multiple columns with same name except for last digits:
Round1, Round2, Round3, Round4...etc
I have also defined a variable that I need to update manually depending on which column Id like to be taken into account in all of my queries instead of updating each one separately (time saving)
$round_number="4"; // round 4 selected for example here
then this is one my queries, I need it to select the round number defined by the variable above, I tried this:
SELECT *, sum(Round'$round_number') as round_total from mytable where group_name LIKE 'fighters%' group by side order by round_total desc
as expected, the above query failed...
Any help is welcome
Thanks fellas EDIT (actual query as is)
$latest_week="4"; // UPDATE ALERT HERE
$result = mysql_query("SELECT *, sum(SR) as SRT, sum(RS'. $latest_week . ') as RST FROM na_july16_resources_v2 where group_name LIKE 'fighters%' group by side order by RST desc")
or die(mysql_error());
Upvotes: 0
Views: 50
Reputation: 3038
You can have a variable be read inside a string by using double quotes. Strings in double quotes (and not single quotes) are parsed for variable names. So you could do this to use the variable inside the query:
"SELECT *, sum(Round$round_number) as round_total from mytable where group_name LIKE 'fighters%' group by side order by round_total desc"
As Jaime said you could also just concatenate the strings using a dot.
Upvotes: 1
Reputation: 1410
In PHP strings are concatenated with a dot.
$sql = 'SELECT *, sum(Round' . $round_number . ') as round_total from mytable where group_name LIKE \'fighters%\' group by side order by round_total desc'
Upvotes: 1