Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> skomplikowane zapytanie
Anna
post
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 21.11.2006

Ostrzeżenie: (0%)
-----


Zapytanie powinno zwracać:
- dane wszystkich userów users_data.*
- sumę punktów zdobytych przez usera w danym teście SUM(users_asks.point)+SUM(answers.point), lub NULL jeśli user testu nie wypełniał
- maksymalną sumę punktów jaką user mógł zdobyć w danym teście

Macie jakieś pomysły? (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Kombinuję w ten sposób:

  1. SELECT users_data. * , ask.suma + ans.suma, ask.max + ans.max
  2. FROM users_data LEFT JOIN (
  3. SELECT SUM( users_asks.point ) AS suma, COUNT( asks.ask_id ) AS max, users_asks.user_id
  4. FROM users_asks, asks
  5. WHERE users_asks.ask_id = asks.ask_id
  6. GROUP BY users_asks.user_id, asks.test_id
  7. ) AS ask
  8. USING ( user_id )
  9. LEFT JOIN (
  10.  
  11. SELECT SUM( answers.point ) AS suma, COUNT( asks.ask_id ) AS max, users_answers.user_id
  12. FROM users_answers, answers, asks
  13. WHERE users_answers.answer_id = answers.answer_id AND answers.ask_id = asks.ask_id
  14. GROUP BY users_answers.user_id, asks.test_id
  15. ) AS ans
  16. USING ( user_id )


(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Ten post edytował Anna 16.03.2007, 17:38:56
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
DeyV
post
Post #2





Grupa: Zarząd
Postów: 2 277
Pomógł: 6
Dołączył: 27.12.2002
Skąd: Wołów/Wrocław




Ok. Wyszło mi z tego coś takiego.

  1. SELECT u_d.user_id,
  2.  
  3. t.test_id,
  4.  
  5. SUM( an.point ) AS MAKS,
  6.  
  7. ( SELECT SUM( users_asks.point ) FROM users_asks LEFT JOIN asks ON (asks.ask_id= users_asks.ask_id )
  8. WHERE users_asks.user_id = u_d.user_id AND asks.test_id = t.test_id ) AS users_asks_point,
  9.  
  10. ( SELECT SUM( answers.point ) FROM users_answers INNER JOIN answers ON ( answers.answer_id = users_answers.answer_id )
  11. INNER JOIN asks ON ( asks.ask_id = answers.ask_id AND asks.test_id = t.test_id )
  12. WHERE users_answers.user_id = u_d.user_id
  13. ) AS users_answers_point
  14.  
  15. FROM tests t
  16. LEFT JOIN users_data u_d ON ( TRUE )
  17.  
  18. LEFT JOIN asks ON ( asks.test_id = t.test_id )
  19. LEFT JOIN answers an ON ( an.ask_id= asks.ask_id )
  20.  
  21. GROUP BY u_d.user_id, t.test_id


Jedna uwaga. U siebie mam tylko MySQL 4, który nie pozwala na SubQuery, więc musiałem przetestować to zapytanie na bazie Postgres. Niestety - nie wiem, co tak naprawdę w tej chwili potrafi MySQL 5 - ponoć dużo, więc zapytanie tego typu powinno chodzić poprawnie. Jednak głowy nie dam (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 17.10.2025 - 20:57