Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. EXPLANATION:
  2.  
  3. 1. One project has a mobilization of crew to work on project
  4. 2. Mobilization action is created, with selected personnel/crew member
  5. 3. Each selected member has selected certificates relevant to the spesific project/mobilization
  6.  
  7.  
  8. GOAL:
  9.  
  10. Get all persons in the mobilization crew, and the certificates selected/relevant for the mobilization of the spesific project.
  11.  
  12.  
  13. -- QUERY
  14.  
  15. EXPLAIN SELECT u.id, u.first_name, c.title AS cert
  16. FROM mobilization_crew_cert AS mc
  17. JOIN certificates AS c ON c.id = mc.certificate_id
  18. JOIN users AS u ON u.id = mc.user_id
  19. JOIN mobilization_crew AS m ON m.mobilization_id = mc.mobilization_id
  20.  
  21. -- WHERE mc.mobilization_id = 1
  22. GROUP BY mc.id
  23.  
  24.  
  25.  
  26. -- TABLES
  27.  
  28. CREATE TABLE `users` (
  29. `id` int(11) unsigned NOT NULL auto_increment,
  30. `username` varchar(15) default NULL,
  31. `password` varchar(40) default NULL,
  32. `created` datetime default '0000-00-00 00:00:00',
  33. `updated` timestamp NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  34. `first_name` varchar(50) default NULL,
  35. `last_name` varchar(50) default NULL,
  36. PRIMARY KEY (`id`)
  37. ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
  38.  
  39. (* project_id is from project table)
  40.  
  41. CREATE TABLE `mobilization` (
  42. `id` int(11) unsigned NOT NULL auto_increment,
  43. `created` datetime default '0000-00-00 00:00:00',
  44. `updated` timestamp NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  45. `project_id` int(11) unsigned default NULL,
  46. PRIMARY KEY (`id`),
  47. KEY `project_id` (`project_id`)
  48. ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
  49.  
  50. CREATE TABLE `mobilization_crew` (
  51. `id` int(11) unsigned NOT NULL auto_increment,
  52. `created` datetime default '0000-00-00 00:00:00',
  53. `updated` timestamp NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  54. `mobilization_id` int(11) unsigned default NULL,
  55. `user_id` int(11) unsigned default NULL,
  56. `share_personalia` tinyint(1) unsigned default '0',
  57. PRIMARY KEY (`id`),
  58. KEY `mobilization_id` (`mobilization_id`),
  59. KEY `user_id` (`user_id`)
  60. ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
  61.  
  62. CREATE TABLE `mobilization_crew_cert` (
  63. `id` int(11) unsigned NOT NULL auto_increment,
  64. `certificate_id` int(11) unsigned default NULL,
  65. `user_id` int(11) unsigned default NULL,
  66. `mobilization_id` int(11) unsigned default NULL,
  67. PRIMARY KEY (`id`),
  68. KEY `user_id` (`user_id`),
  69. KEY `certificate_id` (`certificate_id`),
  70. KEY `mobilization_id` (`mobilization_id`)
  71. ) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
  72.  
  73. CREATE TABLE `certificates` (
  74. `id` int(11) unsigned NOT NULL auto_increment,
  75. `user_id` int(11) unsigned default NULL,
  76. `title` varchar(255) default NULL,
  77. `body` text,
  78. `valid` tinyint(1) unsigned default '0',
  79. `valid_to` date default NULL,
  80. `notice` tinyint(1) unsigned default '0',
  81. `created` datetime default '0000-00-00 00:00:00',
  82. `updated` timestamp NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  83. PRIMARY KEY (`id`)
  84. ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
Add Comment
Please, Sign In to add comment