Author Topic: How to retrieve column names ordered by value and output the result with echo  (Read 332 times)

0 Members and 1 Guest are viewing this topic.

Offline EliteIntegrityTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Hi,

Server version - 5.0.67
the statement - that's what I hope to find out
any errors - na
currently doing not to my liking - na
tried so far - complicated sorting (for a beginner) using PHP want to use query instead.
What I want to achieve -

I want to be able retrieve column names from a single specified row ordered by the numerical value within those columns.

e.g if I have exam results table with a row (history 50, ict 100, art 70) for a student (bill).

 I want the output of the query to be the column names ordered by the value they hold, i.e.

ict art history


Many thanks.

John

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
can you give use the outpuy of:
1) desc table  ( or output of SHOW CREATE TABLE ....)
2) the output of a single row of your data ?

Than, if that is know, the answer should not be hard.
(but i think it will be a PHP answer, and ot a MYSQL answer)
« Last Edit: March 20, 2010, 11:38:23 AM by jskywalker »

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Yeah, you can't do that in mysql.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline EliteIntegrityTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Thanks v-much. PHP was the solution.

Exploring your suggestions led me to discover mysql_field_name($result, x)
And the final solution.

...
$query="SELECT art, ict, history FROM results WHERE name='$name'";
...
$results_array=array(mysql_result($result,0,art),mysql_result($result,0,ict),mysql_result($result,0,history));
$column_names_array=array(mysql_field_name($result, 0),mysql_field_name($result, 1),mysql_field_name($result, 2));

array_multisort($results_array,$column_names_array);

print_r($column_names_array);

Thanks so much again.

John