2015-05-04 39 views
0

我有三個表得到從2個表分組數據與甲骨文加入DB使用JSP

"congers(code_conger primary key,date_depart,date_retour,duree)"

"ouvriers_congers(#idouvrier, #idconger)"

"ouvriers(matricule,nom,prenom)"

什麼我想要我期從他們的工人報「NOM」和「prenom」表康吉爾斯獲取數據並通過idconger

 <% ResultSet rsS = st.executeQuery("select o.matricule,o.nom,o.prenom, c.*, oc.* from ouvriers o, congers c, ouvriers_conger oc where c.code_conger = oc.idconger and oc.idouvrier = o.matricule"); 

     while(rsS.next()){ 
     int cd = rsS.getInt("code_conger"); 
     %> 
      <tr> 
         <td><%= cd %></td> 
         <td><%= rsS.getDate("date_depart") %></td> 
         <td><%= rsS.getDate("date_retour") %></td> 
         <td><%= rsS.getInt("duree") %></td> 
         <td><%= rsS.getString("nom") %></td> 
      </tr> 
        <% } %> 

分組,但它告訴我這樣

 
1 05/01/2015 05/05/2015 4 adil 

1 05/01/2015 05/05/2015 4 souf 

2 08/20/2015 08/30/2015 9 smith 

的數據,你看到的結果ISN 「T由 「code_conger」

電郵宣傳我試圖這組它:

select o.*, c.*, oc.idconger,oc.idouvrier from ouvriers o, congers c, ouvriers_conger oc where c.code_conger = oc.idconger and oc.idouvrier = o.matricule group by oc.idconger

它給我一個錯誤:ORA-00979:不是GROUP BY表達式

的結果,我希望得到的是:

 
1 05/01/2015 05/05/2015 4 -adil 
            -souf 

2 08/20/2015 08/30/2015 9 smith 

這裏是測試用的DDL語句

create table congers 
(
    code_conger NUMBER(5) PRIMARY KEY, 
    date_depart DATE DEFAULT (sysdate), 
    date_retour DATE DEFAULT (sysdate), 
    duree NUMBER(5) 
); 
create table ouvriers_congers 
(
    idouvrier NUMBER(5), 
    idconger NUMBER(5), 
    CONSTRAINT ouvriers_congers_pk PRIMARY KEY (idouvrier, idconger) 
); 
create table ouvriers 
(
    matricule NUMBER(5) PRIMARY KEY, 
    nom VARCHAR2(15), 
    prenom VARCHAR2(15) 
); 
INSERT INTO congers VALUES (1,to_date('05/01/2015','MM/DD/YYYY'), 
        to_date('05/05/2015','MM/DD/YYYY'), 
        4); 
INSERT INTO congers VALUES (2,to_date('08/20/2015','MM/DD/YYYY'), 
        to_date('08/30/2015','MM/DD/YYYY'), 
        9); 
INSERT INTO ouvriers VALUES (1,'adil','adil'); 
INSERT INTO ouvriers VALUES (2,'souf','souf'); 
INSERT INTO ouvriers VALUES (3,'smith','smith'); 
INSERT INTO ouvriers_congers VALUES (1,1); 
INSERT INTO ouvriers_congers VALUES (2,1); 
INSERT INTO ouvriers_congers VALUES (3,2); 

回答

0

我解決了我的自我:

<tbody> 
        <% 
         Statement st2 = Connect.connecter(); 
         ResultSet rsS = st.executeQuery("select * from congers"); 
         while(rsS.next()){ 
         int cd = rsS.getInt("code_conger"); 
        %> 

        <tr> 
         <td><%= cd %></td> 
         <td><%= rsS.getDate("date_depart") %></td> 
         <td><%= rsS.getDate("date_retour") %></td> 
         <td><%= rsS.getInt("duree") %></td> 
         <td> 
          <% 
          ResultSet rs2 = st2.executeQuery("select o.nom,o.prenom,o.matricule from ouvriers o,ouvriers_conger oc where oc.idconger = "+cd+" and oc.idouvrier = o.matricule"); 
          while(rs2.next()){ 
           out.print("- "+rs2.getString("nom")+" "+rs2.getString("prenom")+"<br />"); 
          } 
          %> 
         </td> 
         <td style="width:200px !important;"><center><a href="#"><img alt="Modifier" src="img/edit.png" /></a>&nbsp;&nbsp;&nbsp;<a href="delete.jsp?op=conge&id=<%= cd %>" style="margin-left:50px;"><img alt="Supprimer" src="img/delete.png" /></a></center></td> 
        </tr> 
        <% } %> 
</tbody> 

這裏我把它保存在這裏以防萬一一個人需要它1天

+0

如果數據庫變大,「select * from congers」可能會變得很危險,你應該找到一種方法來限制或分頁 – Robson

+0

我沒有使用分頁...因爲這不會返回一個大數據(最多50個結果或更少) – ADiL

0

可以使用LISTAGG從Oracle:http://www.techonthenet.com/oracle/functions/listagg.php

select 
    OC.IDCONGER, 
    C.DATE_DEPART, 
    C.DATE_RETOUR, 
    C.DUREE, 
    LISTAGG(o.nom||' '||O.PRENOM,',') WITHIN GROUP(ORDER BY O.NOM) AS ouvriers 
FROM 
    congers C, 
    ouvriers_congers OC, 
    ouvriers O 
WHERE 
    OC.IDCONGER = C.CODE_CONGER 
    AND OC.idouvrier = O.matricule 
group by (OC.idconger,C.DATE_DEPART,C.DATE_RETOUR,C.DUREE) 

或MSSQL https://msdn.microsoft.com/it-it/library/ms178107.aspx使用FOR XML
或從MySQL使用GROUP_CONCAT():http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_group-concat
PS:你應分頁數據,以便在數據庫變大時不會得到1203487187124結果

+0

嘗試過它,但仍然得到重複的數據... – ADiL

+0

@ADiL你好男人,我測試了查詢並更新了答案。現在它可以工作 – Robson

+0

@ADiL我在這裏使用這個工具來測試:http://sqlfiddle.com/#!4/482f8/1/0 – Robson