Guest User

Untitled

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