Good evening friends, I have days killing me with an SQL that I do not know how to do, I tell you I am developing a system for the management of universities, and I am trapping in the list of available subjects. I have these tables:
CREATE TABLE EST_CARRERAS (
CCARRERA CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CODIGO DESCRIPCION_CORTA DEFAULT 'NONE' NOT NULL /* DESCRIPCION_CORTA = VARCHAR(50) */,
DESCRIPCION DESCRIPCION_MEDIA NOT NULL /* DESCRIPCION_MEDIA = VARCHAR(120) */,
ESTATUS LOGICO DEFAULT 1 NOT NULL /* LOGICO = INTEGER DEFAULT 1 NOT NULL */
);
CREATE TABLE EST_MATERIAS (
CMATERIA CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CODIGO DESCRIPCION_CORTA DEFAULT 'NONE' /* DESCRIPCION_CORTA = VARCHAR(50) */,
DESCRIPCION DESCRIPCION_MEDIA NOT NULL /* DESCRIPCION_MEDIA = VARCHAR(120) */
);
CREATE TABLE EST_MAT_CAR (
CMATE CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CMATERIA CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CCARRERA CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
SEMESTRE INTEGER DEFAULT '0' NOT NULL
);
CREATE TABLE EST_MAT_PRES (
CPRESLA CODIGO /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CMATERIA CODIGO /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CPRESLACION CODIGO /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CCARRERA CODIGO /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CTIPO CODIGO /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CANT MONTOS /* MONTOS = DOUBLE PRECISION DEFAULT 0.0 */
);
CREATE TABLE EST_INSCRIPCION (
CINSCRIPCION CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CCARRERA CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CPERIODO CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CALUMNO CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
FINSCRIPCION FECHA NOT NULL /* FECHA = DATE */
);
CREATE TABLE EST_INSCRIPCION_DET (
CINSCRIPCION_DET CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CINSCRIPCION CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CMATE CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
CSECCION CODIGO NOT NULL /* CODIGO = INTEGER DEFAULT 0 NOT NULL */,
NOTA MONTOS /* MONTOS = DOUBLE PRECISION DEFAULT 0.0 */
);
Then the first table manages the races, the 2nd the subjects, the 3rd the relation matter - career (cmate) the 4th table the priority of the subjects (cmateria: matter to see or evaluate, cpreslacion: matter that had to be approved to see the cmateria, ctype = if it is 2 CANT that are the number of UC approved), the next 2 are the header and the details of the inscriptions, then I made a small example where to an enrollment record (est_encripcion ) I inserted 3 lines (where I repeated 1 subject 2 times) to verify that the selection brings me the available subjects of a career in question:
SELECT
MC.CMATE,
M.CMATERIA,
M.DESCRIPCION,
C.CCARRERA,
C.DESCRIPCION
FROM EST_MAT_CAR MC
INNER JOIN EST_MATERIAS M ON MC.CMATERIA=M.CMATERIA
INNER JOIN EST_CARRERAS C ON MC.CCARRERA=C.CCARRERA
LEFT JOIN (SELECT
DISTINCT(ID.CMATE)
FROM
EST_INSCRIPCION_DET ID
INNER JOIN EST_INSCRIPCION I ON ID.CINSCRIPCION=I.CINSCRIPCION
WHERE ID.NOTA > 10
AND I.CALUMNO=1
AND I.CCARRERA=1) ID2 ON ID2.CMATE=MC.CMATE
WHERE ID2.CMATE IS NULL
AND C.CCARRERA=1
The select brings me all the subjects that the student can see, but it is bringing me a subject that I should be able to see since it "clashes" with one that has not been approved, (in this case est_mat_pres intervenes) but I do not know how to do it said select to bring me the available subjects and if one of the subjects appears in the field cpreslacion, do not show me the field in the list.
I do not know if they understand me, but I'm very involved and I can not get the answer. The system I am doing in PHP and I presume that I will have to do a kind of LOOP, but I was looking for the most effective way to do it through a SELECT.