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
Anna
post
Post #2





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

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


Ależ proszę...

  1. --
  2. -- Struktura tabeli dla `answers`
  3. --
  4. CREATE TABLE `answers` (
  5. `answer_id` int(11) NOT NULL AUTO_INCREMENT,
  6. `ask_id` int(11) NOT NULL,
  7. `answer` text,
  8. `point` tinyint(4) DEFAULT NULL,
  9. `enable` tinyint(4) DEFAULT NULL,
  10. PRIMARY KEY (`answer_id`),
  11. KEY `answers_FKIndex1` (`ask_id`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
  13. -- --------------------------------------------------------
  14. --
  15. -- Struktura tabeli dla `asks`
  16. --
  17. CREATE TABLE `asks` (
  18. `ask_id` int(11) NOT NULL AUTO_INCREMENT,
  19. `test_id` int(11) NOT NULL,
  20. `ask` text,
  21. `enable` tinyint(4) DEFAULT NULL,
  22. PRIMARY KEY (`ask_id`),
  23. KEY `asks_FKIndex1` (`test_id`)
  24. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
  25. -- --------------------------------------------------------
  26. --
  27. -- Struktura tabeli dla `tests`
  28. --
  29. CREATE TABLE `tests` (
  30. `test_id` int(11) NOT NULL AUTO_INCREMENT,
  31. `name` varchar(45) DEFAULT NULL,
  32. `timeout` int(11) DEFAULT NULL,
  33. `enable` tinyint(4) DEFAULT NULL,
  34. PRIMARY KEY (`test_id`)
  35. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  36. -- --------------------------------------------------------
  37. --
  38. -- Struktura tabeli dla `users`
  39. --
  40. CREATE TABLE `users` (
  41. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  42. `login` varchar(45) DEFAULT NULL,
  43. `passwd` varchar(45) DEFAULT NULL,
  44. `auth` tinyint(4) DEFAULT NULL,
  45. PRIMARY KEY (`user_id`)
  46. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  47. -- --------------------------------------------------------
  48. --
  49. -- Struktura tabeli dla `users_answers`
  50. --
  51. CREATE TABLE `users_answers` (
  52. `answer_id` int(11) NOT NULL,
  53. `user_id` int(11) NOT NULL,
  54. PRIMARY KEY (`answer_id`,`user_id`),
  55. KEY `users_answers_FKIndex1` (`user_id`),
  56. KEY `users_answers_FKIndex2` (`answer_id`)
  57. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  58. -- --------------------------------------------------------
  59. --
  60. -- Struktura tabeli dla `users_asks`
  61. --
  62. CREATE TABLE `users_asks` (
  63. `user_id` int(11) NOT NULL,
  64. `ask_id` int(11) NOT NULL,
  65. `answer` text,
  66. `point` tinyint(4) DEFAULT NULL,
  67. PRIMARY KEY (`user_id`,`ask_id`),
  68. KEY `users_asks_FKIndex1` (`user_id`),
  69. KEY `users_asks_FKIndex2` (`ask_id`)
  70. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  71. -- --------------------------------------------------------
  72. --
  73. -- Struktura tabeli dla `users_data`
  74. --
  75. CREATE TABLE `users_data` (
  76. `user_id` int(11) NOT NULL,
  77. `firstname` varchar(45) DEFAULT NULL,
  78. `surname` varchar(45) DEFAULT NULL,
  79. `email` varchar(45) DEFAULT NULL,
  80. `phone` varchar(45) DEFAULT NULL,
  81. PRIMARY KEY (`user_id`),
  82. KEY `users_data_FKIndex1` (`user_id`)
  83. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


Myślałam, że uda się coś "na sucho" wykombinować (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
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: 12.10.2025 - 08:44