query or cursor mysql

0

I have a problem with this type of query or it would be better with a cursor

I leave you an example table:

*create table table11(
cod_table int,
dni varchar(9),
doc_slug varchar(100),
doc_tipo int,
fecha_vecimiento date,
fecha_carga date
)
insert into table11 values(12,123456789,'BP',1,'2018-04-20','2018-04-1');

insert into table11 values(15342,123456789,'BP',1,'2015-04-20','2015-04-1');
insert into table11 values(14322,123456789,'BP',1,'2015-04-20','2015-04-1');
insert into table11 values(18762,123456789,'BP',1,'2017-04-20','2017-04-1');

insert into table11 values(16772,123456789,'BP2',2,'2015-05-20','2015-04-1');
insert into table11 values(16992,123456789,'BP2',2,'2018-04-20','2017-04-1');
insert into table11 values(13322,123456789,'BP2',2,'2013-05-20','2013-04-1');

insert into table11 values(12534,123456789,'BP3',3,'2017-09-20','2017-08-1');

insert into table11 values(12588,123456789,'BP3',3,'2017-09-20','2017-08-1');
insert into table11 values(12599,123456789,'BP3',3,'2018-04-20','2018-04-1');
insert into table11 values(12555,123456789,'BP3',3,'2017-09-20','2017-08-1');

insert into table11 values(12234,123456789,'BP4',4,'2018-01-20','2018-01-1');

insert into table11 values(12646544,123456789,'BP4',4,'2016-01-20','2016-01-1');
insert into table11 values(176764,123456789,'BP4',4,'2017-01-20','2017-01-1');
insert into table11 values(135334,123456789,'BP4',4,'2012-01-20','2012-01-1');

insert into table11 values(11152,213245451,'PDF',5,'2018-04-20','2018-04-1');

insert into table11 values(143352,213245451,'PDF',5,'2017-04-20','2017-04-1');
insert into table11 values(1545452,213245451,'PDF',5,'2016-04-20','2016-04-1');
insert into table11 values(113432152,213245451,'PDF',5,'2015-04-20','20185-04-1');

insert into table11 values(43512,213245451,'PDF1',6,'2017-07-20','2017-07-1');
insert into table11 values(43241,213245451,'PDF2',7,'2017-04-20','2017-04-1');
insert into table11 values(13422,213245451,'PDF3',8,'2016-05-20','2016-05-1');
insert into table11 values(12543,464641663,'CARTERA',9,'2018-04-20','2018-04-1');
insert into table11 values(14322,464641663,'CARTERA1',10,'2017-03-20','2017-03-1');
insert into table11 values(16962,464641663,'CARTERA2',11,'2017-02-20','2017-02-1');
insert into table11 values(17872,464641663,'CARTERA3',12,'2016-04-20','2016-04-1');
insert into table11 values(18582,587481521,'SALUD',12,'2018-04-20','2018-04-1');
insert into table11 values(12232,587481521,'SALUD1',13,'2017-03-20','2017-03-1');
insert into table11 values(12979,587481521,'SALUD2',14,'2017-04-20','2017-04-1');
insert into table11 values(15472,646456332,'CINTA',15,'2018-04-20','2018-04-1');
insert into table11 values(17852,646456332,'CINTA1',16,'2018-04-20','2018-04-1');
insert into table11 values(13732,646456332,'CINTA2',17,'2017-04-20','2017-04-1');
insert into table11 values(12745,646456332,'CINTA3',18,'2017-02-20','2017-02-1');
insert into table11 values(15472,765443252,'FACTURA',19,'2018-04-20','2018-04-1');
insert into table11 values(14562,765443252,'FACTURA1',20,'2017-06-20','2017-06-1');
insert into table11 values(61287,765443252,'FACTURA2',21,'2017-07-20','2017-07-1');
insert into table11 values(12987,765443252,'FACTURA3',22,'2016-04-20','2016-04-1');
insert into table11 values(12586,788888533,'ALLIAS',22,'2017-04-20','2017-04-1');
insert into table11 values(12898,699999323,'CORTEJO',23,'2017-03-20','2017-03-1');
insert into table11 values(12995,753333533,'SANDIA',24,'2017-09-20','2017-09-1');
insert into table11 values(12987,111353533,'BANANA',25,'2017-06-20','2017-06-1');
insert into table11 values(19782,722353533,'FRESA',26,'2016-01-20','2016-01-1');
insert into table11 values(12878,764353533,'ROCOTO',27,'2016-07-20','2016-07-1');
insert into table11 values(12990,957753533,'CEVICHE',28,'2016-08-20','2016-08-1');

What I want to do is:

As you can see there are the same DNI for different doc and different dates and the same different documents than documents.

My problem is I want a query that shows me all DNI and doc that is not in the range of 2018 and month 04 but as there are several with the same doc and DNI

I made a not between but also if there is a same DNI and same doc a date that is of the year 2018 and month of 04 discard that DNI and that only shows the DNI that is not 2018 of 04

(but if the dni and doc there are different dates in 2017 and another 2018 it's enough that I have 2018 and month 04 discard that ID and only show the ones I do not have 2018 and 04)

I await your answer thank you.

This is a test table, the original has a thousand times more records

    
asked by danny 13.06.2018 в 22:50
source

0 answers