我有三张桌子
"congers(code_conger primary key,date_depart,date_retour,duree)"
和
"ouvriers_congers(#idouvrier, #idconger)"
和
"ouvriers(matricule,nom,prenom)"
我想要的是从表格中获取数据及其OUVRIER" 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
如您所见,结果未被&#34; code_conger&#34;
分组我试过这个分组:
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 :(得分: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> <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)
你可以使用oracle的LISTAGG: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结果