Ir ao conteúdo
  • Cadastre-se

Selecionar todos que estão em uma condição


Posts recomendados

Pessoal, não sei se dará para explicar muito bem...

 

 

Tenho uma tabela de alunos e uma tabela de notas... relacionados pelo id_aluno...

 

Na tabela notas, tenho um campo que é o id da matéria, ou seja, para que o aluno tenha determinada nota em determinada matéria

 

Preciso selecionar todos os alunos que tiveram nota maior que 20, por exemplo... para isso usei tranquilamente o having... where...

 

 

A questão é que preciso selecionar todos os alunos que tem nota maior que vinte, so que tem que ser em todas as matérias... ou seja, se tem 5 matérias, e o aluno tem notas maiores que 20 em quatro matérias, ele não pode entrar na consulta, pois teria que ter nota maior que 20 em todas...

Link para o comentário
Compartilhar em outros sites

  • 2 semanas depois...

Minha estrutura é assim..

Como fica essa união de duas consultas?

*tb_alunosid_alunonomeid_turma*tb_turmasid_turmaturma*tb_disciplinasid_disciplinasid_disciplinadisciplina*tb_disc_turmaid_disc_turmaid_discid_turma*tb_avaliacoesid_avaliacaoid_disc_turmatrimestrevalor*tb_notasid_notaid_avaliacaoid_alunonota
select al.id_aluno, al.aluno,d.disciplina,sum(nt.nota) as tt_notafrom tb_notas ntinner join tb_avaliacoes avon av.id_avaliacao = nt.id_avaliacaoinner join tb_alunos alon al.id_aluno = nt.id_alunoinner join tb_disc_turma dton dt.id_disc_turma = av.id_disc_turmainner join tb_disciplinas don d.cod_disciplina = dt.cod_disciplinawhere av.trimestre = 1 group by al.id_aluno, dt.id_disc_turma
Link para o comentário
Compartilhar em outros sites

Exportei as tabelas... 

-- phpMyAdmin SQL Dump-- version 4.0.4-- http://www.phpmyadmin.net---- Máquina: localhost-- Data de Criação: 17-Set-2015 às 11:04-- Versão do servidor: 5.6.12-log-- versão do PHP: 5.4.16SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;---- Estrutura da tabela `tb_alunos`--CREATE TABLE IF NOT EXISTS `tb_alunos` (  `cod_aluno` bigint(20) NOT NULL AUTO_INCREMENT,  `ordem` int(2) NOT NULL,  `aluno` varchar(200) NOT NULL,  `obs` text NOT NULL,  `status` int(1) NOT NULL,  `cod_turma` bigint(20) NOT NULL,  `cod_op` bigint(20) NOT NULL,  PRIMARY KEY (`cod_aluno`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2572 ;-- ------------------------------------------------------------ Estrutura da tabela `tb_avaliacoes`--CREATE TABLE IF NOT EXISTS `tb_avaliacoes` (  `cod_avaliacao` bigint(20) NOT NULL AUTO_INCREMENT,  `nome_avaliacao` varchar(200) NOT NULL,  `cod_disc_prof_turma` bigint(20) NOT NULL,  `cod_trimestre` bigint(20) NOT NULL,  `cod_grupo_nota` bigint(20) NOT NULL,  `valor_avaliacao` decimal(6,2) NOT NULL,  `desc_avaliacao` text NOT NULL,  `objetivo_avaliacao` text NOT NULL,  `obs_avaliacao` text NOT NULL,  `cod_tipo_avaliacao` int(11) NOT NULL,  `cod_ref_tipo_avaliacao` int(11) NOT NULL,  `data_avaliacao` date NOT NULL,  `data_criacao` date NOT NULL,  `status_avaliacao` int(1) NOT NULL,  `status_lancar_nota` int(1) NOT NULL,  PRIMARY KEY (`cod_avaliacao`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6095 ;-- ------------------------------------------------------------ Estrutura da tabela `tb_disciplinas`--CREATE TABLE IF NOT EXISTS `tb_disciplinas` (  `cod_disciplina` bigint(20) NOT NULL AUTO_INCREMENT,  `disciplina` varchar(200) NOT NULL,  `abrev` varchar(8) NOT NULL,  PRIMARY KEY (`cod_disciplina`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=68 ;-- ------------------------------------------------------------ Estrutura da tabela `tb_disc_prof_turma`--CREATE TABLE IF NOT EXISTS `tb_disc_prof_turma` (  `cod_disc_prof_turma` bigint(20) NOT NULL AUTO_INCREMENT,  `cod_professor` bigint(20) NOT NULL,  `cod_disciplina` bigint(20) NOT NULL,  `cod_turma` bigint(20) NOT NULL,  `ch` bigint(20) NOT NULL,  `cod_op` bigint(20) NOT NULL,  PRIMARY KEY (`cod_disc_prof_turma`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=810 ;-- ------------------------------------------------------------ Estrutura da tabela `tb_notas`--CREATE TABLE IF NOT EXISTS `tb_notas` (  `cod_nota` int(11) NOT NULL AUTO_INCREMENT,  `valor_nota` decimal(6,2) NOT NULL,  `valor_nota_rp` decimal(6,2) NOT NULL,  `valor_nota_maior` decimal(6,2) NOT NULL,  `nfez` int(11) NOT NULL,  `cod_aluno` int(11) NOT NULL,  `cod_avaliacao` int(11) NOT NULL,  `data_cadastro` datetime NOT NULL,  `obs` text NOT NULL,  `status_nota` int(1) NOT NULL,  PRIMARY KEY (`cod_nota`),  UNIQUE KEY `cod_aluno` (`cod_aluno`,`cod_avaliacao`),  UNIQUE KEY `cod_aluno_2` (`cod_aluno`,`cod_avaliacao`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=209274 ;-- ------------------------------------------------------------ Estrutura da tabela `tb_turmas`--CREATE TABLE IF NOT EXISTS `tb_turmas` (  `cod_turma` bigint(20) NOT NULL AUTO_INCREMENT,  `turma` varchar(200) NOT NULL,  `serie` varchar(10) NOT NULL,  `turno` varchar(50) NOT NULL,  `nivel` varchar(50) NOT NULL,  PRIMARY KEY (`cod_turma`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Link para o comentário
Compartilhar em outros sites

  • Moderador

@deguiannave

 

Ao meu ver, não precisa o campo id_turma na tabela alunos

Pois o correto seria fazer o que o  amigo já sugeriu.. criar uma tabrla N:N  entre turma e aluno...

 

Se quiser ir mais a fundo.. pode incluir o id_professor  no N:N  aí tu pensa assim:

 

Turma tem professor e alunos...  aí tu elimina o `cod_turma` bigint(20) NOT NULL,   da tabela tb_disc_prof_turma

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...