2

I wanted to write several mysql queries to retrieve data from mysql database.

If I explain the problem, there are 136 columns in the data base table. Table is used to store students' information and their skill categories. There are about 6 main categories and all 136 sub categories lay withing them. If the main category is Mathematics there are subcategories such as Applied maths, Pure Mathematics and etc. All the information are included in one table and there is no normalization.

Students' skill level is measured from 1-5. 5 is maximum. If I passe a student Name in to mysql query, I want to print his main categories and sub categories in descending order. Please note that any student can follow more than one major category.

Last Name       Web technologies - HTML Web technologies - CSS  Web technologies - XML  Web technologies - JavaScript   Web technologies - J-Query  ......
Bill            3   1   4   na  na  1   3   na  na  na  na  na  na  na  na  na
Arun            1   5   1   3   1   2   1   na  na  3   1   5   1   1   na
Soma            na  na  na  na  na  na  na  na  na  na  na  na  na  na  na  na

If I select Arun, I want to show skill level as 5 5 3 3 3 1111111 and respective skill category.

4

1 回答 1

1
SELECT *
FROM (SELECT "Web Technologies HTML", skill_web_html skill_level
      FROM student_skills
      WHERE student_id = @id
      UNION
      SELECT "Web Technologies CSS", skill_web_css
      FROM student_skills
      WHERE student_id = @id
      UNION
      ...
      SELECT "Web Technologies J-Query", skill_web_jquery
      FROM student_skills
      WHERE student_id = @id
      UNION
      ...) all_skills
ORDER BY skill_level DESC
于 2013-06-10T15:25:55.447 回答