Guest User

Untitled

a guest
Jul 9th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. id | id_course | lesson_title
  2. 1 | 3 | Урок 1
  3. 2 | 3 | Урок 2
  4. 3 | 3 | Урок 3
  5. 4 | 4 | Урок 4
  6. 5 | 4 | Урок 5
  7.  
  8. id | id_lesson | email | date
  9. 43 | 1 | 123@gmail.com | 2018-07-09 13:17:05
  10. 44 | 2 | 123@gmail.com | 2018-07-09 13:18:05
  11. 45 | 3 | 123@gmail.com | 2018-07-09 13:19:05
  12.  
  13. function total_score($id){
  14. global $connection;
  15. $query = "SELECT l.id_course, h.email, MAX(h.date) FROM lesson l LEFT JOIN history h ON h.id_lesson = l.id WHERE l.id_course= '$id' GROUP BY l.id_course, h.email HAVING SUM(h.id_lesson IS NULL) = 0";
  16. $res = mysqli_query($connection, $query);
  17. $total_score = array();
  18. while($row = mysqli_fetch_assoc($res)){
  19. $total_score[$row['email']] = $row;
  20. }
  21. return $total_score;
  22. }
  23.  
  24. SELECT l.id_course, h.email, MAX(h.date)
  25. FROM lesson l
  26. LEFT JOIN history h ON h.id_lesson = l.id
  27. GROUP BY l.id_course, h.email
  28. HAVING SUM(h.id_lesson IS NULL) = 0
  29.  
  30. create table `lesson`
  31. (
  32. `id` int,
  33. `id_course` int,
  34. `lesson_title` text
  35. );
  36.  
  37. create table history
  38. (
  39. `id` int,
  40. `id_lesson` int,
  41. `email` text,
  42. `date` date
  43. );
  44.  
  45. Insert into `lesson` (`id`,`id_course`,`lesson_title`) values
  46. (1, 3, 'Урок 1'),
  47. (2, 3, 'Урок 2'),
  48. (3, 3, 'Урок 3'),
  49. (4, 4, 'Урок 4'),
  50. (5, 4, 'Урок 5')
  51. ;
  52.  
  53. Insert into `history` (`id`,`id_lesson`,`email`,`date`) values
  54. (43, 1, '100@gmail.com', now()),
  55. (44, 2, '100@gmail.com', now()),
  56. (45, 3, '100@gmail.com', now()),
  57. (46, 4, '110@gmail.com', now()),
  58. (47, 5, '110@gmail.com', now()),
  59. (48, 1, '200@gmail.com', now()),
  60. (49, 2, '200@gmail.com', now()),
  61. (50, 1, '300@gmail.com', now()),
  62. (51, 4, '300@gmail.com', now())
  63. ;
  64.  
  65. select * from `lesson`;
  66.  
  67. select * from `history`;
  68.  
  69. select * from `history`;
  70.  
  71. select distinct
  72. h.`email`,
  73. l.`id_course`
  74. from `lesson` l
  75. join `history` h on l.`id` = h.`id_lesson`
  76.  
  77. select distinct
  78. h.`email`,
  79. l.`id_course`
  80. from `lesson` l
  81. join `history` h on l.`id` = h.`id_lesson`
  82.  
  83. select
  84. cr.`email`,
  85. cr.`id_course`,
  86. L0.`id` as id_lesson
  87. from
  88. (
  89. select distinct
  90. h.`email`,
  91. L.`id_course`
  92. from `lesson` L
  93. join `history` h on L.`id` = h.`id_lesson`
  94. ) as cr
  95. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  96. order by
  97. cr.`email` asc,
  98. cr.`id_course` asc,
  99. L0.`id` asc
  100.  
  101. select
  102. cr.`email`,
  103. cr.`id_course`,
  104. L0.`id` as id_lesson
  105. from
  106. (
  107. select distinct
  108. h.`email`,
  109. L.`id_course`
  110. from `lesson` L
  111. join `history` h on L.`id` = h.`id_lesson`
  112. ) as cr
  113. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  114. order by
  115. cr.`email` asc,
  116. cr.`id_course` asc,
  117. L0.`id` asc
  118.  
  119. -- полная таблица прохождения/непрохождения уроков
  120. select
  121. L1.`email`,
  122. L1.`id_course`,
  123. L1.`id_lesson`,
  124. H1.`email`
  125. from
  126. (
  127. select
  128. cr.`email`,
  129. cr.`id_course`,
  130. L0.`id` as id_lesson
  131. from
  132. (
  133. select distinct
  134. h.`email`,
  135. L.`id_course`
  136. from `lesson` L
  137. join `history` h on L.`id` = h.`id_lesson`
  138. ) as cr
  139. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  140. ) L1
  141. left outer join `history` H1
  142. on
  143. H1.`email` = L1.`email`
  144. and H1.`id_lesson` = L1.`id_lesson`
  145. order BY
  146. L1.`email`,
  147. L1.`id_course`,
  148. L1.`id_lesson`
  149.  
  150. -- полная таблица прохождения/непрохождения уроков
  151. select
  152. L1.`email`,
  153. L1.`id_course`,
  154. L1.`id_lesson`,
  155. H1.`email`
  156. from
  157. (
  158. select
  159. cr.`email`,
  160. cr.`id_course`,
  161. L0.`id` as id_lesson
  162. from
  163. (
  164. select distinct
  165. h.`email`,
  166. L.`id_course`
  167. from `lesson` L
  168. join `history` h on L.`id` = h.`id_lesson`
  169. ) as cr
  170. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  171. ) L1
  172. left outer join `history` H1
  173. on
  174. H1.`email` = L1.`email`
  175. and H1.`id_lesson` = L1.`id_lesson`
  176. order BY
  177. L1.`email`,
  178. L1.`id_course`,
  179. L1.`id_lesson`
  180.  
  181. -- ученик не прошел весь курс
  182. select distinct
  183. L1.`email`,
  184. L1.`id_course`
  185. from
  186. (
  187. select
  188. cr.`email`,
  189. cr.`id_course`,
  190. L0.`id` as id_lesson
  191. from
  192. (
  193. select distinct
  194. h.`email`,
  195. L.`id_course`
  196. from `lesson` L
  197. join `history` h on L.`id` = h.`id_lesson`
  198. ) as cr
  199. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  200. ) L1
  201. left outer join `history` H1
  202. on
  203. H1.`email` = L1.`email`
  204. and H1.`id_lesson` = L1.`id_lesson`
  205. where
  206. H1.`email` is null
  207.  
  208. -- ученик не прошел весь курс
  209. select distinct
  210. L1.`email`,
  211. L1.`id_course`
  212. from
  213. (
  214. select
  215. cr.`email`,
  216. cr.`id_course`,
  217. L0.`id` as id_lesson
  218. from
  219. (
  220. select distinct
  221. h.`email`,
  222. L.`id_course`
  223. from `lesson` L
  224. join `history` h on L.`id` = h.`id_lesson`
  225. ) as cr
  226. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  227. ) L1
  228. left outer join `history` H1
  229. on
  230. H1.`email` = L1.`email`
  231. and H1.`id_lesson` = L1.`id_lesson`
  232. where
  233. H1.`email` is null
  234.  
  235. -- РЕЗУЛЬТАТ: пользователи прошедшие курс
  236.  
  237. select
  238. L3.`email`,
  239. L3.`id_course`
  240. from
  241. (
  242. select distinct
  243. h.`email`,
  244. l.`id_course`
  245. from `lesson` l
  246. join `history` h on l.`id` = h.`id_lesson`
  247. ) L3 left outer join
  248. (
  249. select distinct
  250. L1.`email`,
  251. L1.`id_course`
  252. from
  253. (
  254. select
  255. cr.`email`,
  256. cr.`id_course`,
  257. L0.`id` as id_lesson
  258. from
  259. (
  260. select distinct
  261. h.`email`,
  262. L.`id_course`
  263. from `lesson` L
  264. join `history` h on L.`id` = h.`id_lesson`
  265. ) as cr
  266. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  267. ) L1
  268. left outer join `history` H1
  269. on
  270. H1.`email` = L1.`email`
  271. and H1.`id_lesson` = L1.`id_lesson`
  272. where
  273. H1.`email` is null
  274. ) L2 on
  275. L2.`email` = L3.`email`
  276. and L2.`id_course` = L3.`id_course`
  277. where
  278. L2.`email` is null
  279.  
  280. -- РЕЗУЛЬТАТ: пользователи прошедшие курс
  281.  
  282. select
  283. L3.`email`,
  284. L3.`id_course`
  285. from
  286. (
  287. select distinct
  288. h.`email`,
  289. l.`id_course`
  290. from `lesson` l
  291. join `history` h on l.`id` = h.`id_lesson`
  292. ) L3 left outer join
  293. (
  294. select distinct
  295. L1.`email`,
  296. L1.`id_course`
  297. from
  298. (
  299. select
  300. cr.`email`,
  301. cr.`id_course`,
  302. L0.`id` as id_lesson
  303. from
  304. (
  305. select distinct
  306. h.`email`,
  307. L.`id_course`
  308. from `lesson` L
  309. join `history` h on L.`id` = h.`id_lesson`
  310. ) as cr
  311. join `lesson` L0 on cr.`id_course` = L0.`id_course`
  312. ) L1
  313. left outer join `history` H1
  314. on
  315. H1.`email` = L1.`email`
  316. and H1.`id_lesson` = L1.`id_lesson`
  317. where
  318. H1.`email` is null
  319. ) L2 on
  320. L2.`email` = L3.`email`
  321. and L2.`id_course` = L3.`id_course`
  322. where
  323. L2.`email` is null
Add Comment
Please, Sign In to add comment