Czesc,
Mam problem z zapytaniem typu crosstab (to w sumie 5 zapytan);
Na wstepie napisze ze te rozwiazania u mnie nie zdaja egzaminu:
http://stackoverflow.com/questions/1238277...-crosstab-query
http://stackoverflow.com/questions/1599709...b-view-in-mysql
Dostaje syntax "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM csre_profile LEFT JOIN csre_profile_data ON csre_profile.csre_profile_id"
ktory nie ma sensu.
Testowe zapytanie ktore proboje wywolac wyglada tak: (jest poprzeinaczane, robilem wszystko zeby najpierw SQL cokolwiek mi zwrocil)
SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( ' GROUP_CONCAT(CASE question_normalized when ', question_normalized , ' then question_value else NULL END) AS `', question_normalized, '`' ) ) INTO @sql FROM csre_profile LEFT JOIN csre_profile_data ON csre_profile.csre_profile_id = csre_profile_data.csre_profile_fk LEFT JOIN csre_question ON csre_question_id=csre_profile_data.csre_question_fk; SET @sql = CONCAT('SELECT csre_profile_id, name, email, ', @sql, ' FROM csre_profile LEFT JOIN csre_profile_data ON csre_profile.csre_profile_id = csre_profile_data.csre_profile_fk GROUP BY csre_profile_id LEFT JOIN csre_question ON csre_question_id=csre_profile_data.csre_question_fk'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Dane:
3 tabele.
csre_profile: csre_profile_id | name | surname .... === tu niejasnosci nie ma
csre_profile_data: csre_profile_data_id | csre_profile_fk | question_fk | question_value === csre_profile_fk to klucz z tabeli glownej, question_fk to id z tabeli ze zbiorem wartosci (ulica, plec itp), question_value to odpowiedz na pytanie (np: kobieta)
csre_question: csre_questio_id | question_name | question_normalized === question_normalized to pytanie znormalizowane bez pl znakow i spacji.
Chcialbym uzyskac swoim zapytaniem taki efekt:
id | name | gender | avatar .....
1 Zoska Kobieta http://costam
Gdzie kolumny gender i avatar sa wartosciami z tabeli csre_profile_data.