SQL algorithm with three identifiers from one table

It will probably be very simple, but I can’t figure out how to get the necessary values ​​from my database with a single query. I just can't figure it out now. I am going to make this request on the CodeIginiter system.

Table Information Design:

CREATE TABLE information (
    planid int(11) NOT NULL,
    production_nr int(11) NOT NULL,
    status int(11) NOT NULL
);

Information table:
enter image description here

Required output: I would like to receive (at best, with only one query, but if this is not possible, then with several) all planid where: ALL of this plan id pruduction_nrs has stats> = 3.

In this case, I will need to get these plandid: 2 and 5, because each of these planid ALL production_nrs has a status greater than or equal to 3.

0
3
select planid, production_nr
from information inf1
where not exists (select 1 from information inf2
                  where inf1.planid = inf2.planid
                  and   status < 3)

, select ( ) :

  • ( PK )

+1

,

SELECT planid , production_nr FROM  information
WHERE production_nr IN(SELECT production_nr FROM information) AND STATUS >=3
0

. .

1) planid, production_nr < 3

select planid 
from information i1
where not exists (
    select 1 from information i2
    where i1.planid = i2.planid
      and i2.planid < 3
) 

2) planid, production_nr production_nr >= 3. ;-)

0

Source: https://habr.com/ru/post/1542593/


All Articles