如何在SQL中制作计算器?

时间:2018-06-05 10:57:01

标签: sql stored-procedures many-to-many

所以我在学校有这种帮助,我应该为一家公司的DPO做一个工具。任务如下:“您需要开发一个应用程序或在线工具,您可以在其中输入法规为公司指定的许多详细信息的状态。该工具的预期用户是数据保护官(DPO) )或负责一般数据投影的员工。该工具应能够处理来自多个公司的输入和对单个公司的多个审计。该工具应能够计算GDPR的合规水平。因此,对于计算器,我想到了一个解决方案,其中给定公司的DPO可以给出12个法规中的一个或者1或0,其中1表示遵守规则而0表示不遵守。所以我想取总和然后除以12,得到合规平均数。但是我如何在SQL中执行此操作? 截至目前,我已经制作了这样的公司,审计师和监管表,如下所示:

    CREATE TABLE IF NOT EXISTS`Companies` (
    `idCompanies` INT NOT NULL,
    `cvr_nr` INT NULL,
    `Dato` DATE NULL,
    PRIMARY KEY (`idCompanies`))
    ENGINE = InnoDB;

    CREATE TABLE IF NOT EXISTS `Auditors` (
      `idAuditors` INT NOT NULL,
      `Auditor_name` VARCHAR(45) NULL,
      PRIMARY KEY (`idAuditors`))
    ENGINE = InnoDB;

insert into auditors values (1, "Lars Larsen");
insert into auditors values (2, "Henrik Andersen");
insert into auditors values (3, "Jens Andersen");

drop table if exists regulations;
CREATE TABLE IF NOT EXISTS `Regulations` (
  `idRegulations` INT NOT NULL auto_increment,
  `Regulation_name` VARCHAR(100) NULL,
  `Regulation_details` VARCHAR(400) NULL,
  PRIMARY KEY (`idRegulations`))
ENGINE = InnoDB;

    insert into regulations values (null, "xx", "xxx");
    insert into regulations values (null, "xx", "xxx");

#The XX are just as examples.

CREATE TABLE IF NOT EXISTS `Companies_has_Regulations` (
  `Companies_idCompanies` INT NOT NULL,
  `Regulations_idRegulations` INT NOT NULL,
  PRIMARY KEY (`Companies_idCompanies`, `Regulations_idRegulations`),
  INDEX `fk_Companies_has_Regulations_Regulations1_idx` (`Regulations_idRegulations` ASC),
  INDEX `fk_Companies_has_Regulations_Companies1_idx` (`Companies_idCompanies` ASC),
  CONSTRAINT `fk_Companies_has_Regulations_Companies1`
    FOREIGN KEY (`Companies_idCompanies`)
    REFERENCES `Companies` (`idCompanies`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Companies_has_Regulations_Regulations1`
    FOREIGN KEY (`Regulations_idRegulations`)
    REFERENCES `Regulations` (`idRegulations`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:0)

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private BCryptPasswordEncoder bCryptPasswordEncoder; @Autowired private DataSource dataSource; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**", "/vendor/**", "/fonts/**").permitAll() .anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout() .permitAll().and().rememberMe().key("uniqueAndSecret"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().usersByUsernameQuery("select email, password, active from user where email=?") .authoritiesByUsernameQuery( "select u.email, r.role from user u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?") .dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder); } } @Configuration public class MvcConfig implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } @Bean public BCryptPasswordEncoder passwordEncoder() { BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); return bCryptPasswordEncoder; } } Regulations条件ID中选择所有行,并过滤相关公司。然后,您获得了所有规则,并且对于每一行,LEFT JOIN Companies_has_Regulations ON中的条目都存在Companies_has_Regulations中其他列中的值。对于来自Companies_has_Regulations的行,其中Regulations中不存在任何条目,这些列具有空值。现在,您可以Companies_has_Regulations使用count()上的ID来获取所有法规的计数,Regulations超过count(),以获得公司遵守的所有法规的计数({ {1}}不计算Regulations_idRegulations)的行数。把它放在一个部门,你就得到了合规率。

count(column)

column IS NULL替换为相应的公司ID。