2017-06-11 14 views
0

ゲット私は私が働いているMySQLデータベースからこのテーブルを持っている:エリア3つ以上の資格の従業員を持っているもの

create table employees (
    num_pass int(5) not null, 
    name varchar(40), 
    primary key (num_pass) 
    )engine=innodb; 

create table laboratories (
    code int(10) not null, 
    name varchar(40), 
    primary key (codi), 
)engine=innodb; 

create table areas (
    code int(5) not null, 
    codeLab int(10) not null, 
    level enum('H','M','L'), 
    primary key (code, codeLab), 
    foreign key (codeLab) references laboratories(code) 
    )engine=innodb; 

create table qualifieds (
    num_pass int(5) not null, 
    area_assigned int(5), 
    lab int(5), 
    primary key (num_pass), 
    foreign key (num_pass) references employees(num_pass), 
    foreign key (area_assigned, lab) references areas (code, codeLab) 
    )engine=innodb; 

今私は以上の3人の資格の従業員を持っているものの領域を取得したいです。具体的には、私は研究所、研究所の名前と一緒に地域コードを取得したい、地域別に注文します。

私は地域のコード

select b.code 
from employees e, areas b, qualifieds q 
where e.num_pass=q.num_pass 
and 3 < (select count(b1.code) 
     from areas b1, qualifieds q1, employers e1 
     where e1.num_pass=q1.num_pass 
     and q1.area_assigned=b1.code 
     and q1.lab=b1.codeLab); 

しかし、私は、従業員の数(I何度でも繰り返し、すべてのエリアコードのリストで得るものすべてを取得するためには、このコマンドを使用しようとしました1,2,3,4,6人のコードを持つエリアがあります。シーケンス1,2,3,4が6回繰り返されます。)必要な情報をどのように入手するかについての考え方はありますか?あなたが参加する追加し、この上で展開する必要がある場合は

+0

3つのうち2つは良いですが、[私は非常に単純なSQLクエリであると思われるMCVEを提供する必要があります](http://meta.stackoverflow.com/questions/333952/why-should-i -provide-an-mcve-for-what-to-me-to-be-a-very-simple-sql-query) – Strawberry

答えて

0

あなたは...

select areas_assigned, lab, count(num_pass) 
    from qualifieds 
    group by areas_assigned, lab 
    having count(num_pass)>3; 

... GROUP BYを使用して、3つの以上のレコードを持つグループを検索することができ

select q.areas_assigned, l.name, count(q.num_pass) 
    join laboratories l on l.lab = q.lab 
    from qualifieds q 
    group by q.areas_assigned, l.name 
    having count(q.num_pass)>3; 
+0

私は何かを変更しましたが、あなたの答えは私に正しい入力を与えました。 (Q.num_pass)> 3; ' 'を選択すると、それは動作します、ありがとう、ありがとう、ありがとうございました。君は! – mao95

関連する問題