PDA

View Full Version : [SQL] Scrivere meglio le mie query


Redvex
18-06-2008, 15:12
Sto facendo un programma in java/jsf/jsp utilizzando mysql come dbms; ora nel get di una variabile eseguo queste 3 query:
public String getNome_matr() {
sql ="SELECT MAX(id) FROM post";
ResultSet rs = sqlCommandBean.executeQuery(sql);
try{
rs.next();
this.id = rs.getInt("MAX(id)");
}catch (Exception e){}
sql="SELECT matr_dipendente FROM inserisce WHERE id_post='"+this.id+"'";
ResultSet rs1 = sqlCommandBean.executeQuery(sql);
try{
rs1.next();
matr_post = rs1.getInt("matr_dipendente");
}catch (Exception e){}
sql ="SELECT nome FROM dipendente WHERE matricola='"+matr_post+"'";
ResultSet rs2 = sqlCommandBean.executeQuery(sql);
try{
rs2.next();
nome_matr= rs2.getString("nome");
}catch(Exception e){}
return nome_matr;
}


Come posso concentrare queste 3 query per avere solo 1 resultset?
Avendo delle buone prestazioni magari

john_revelator
18-06-2008, 15:46
Potresti provare a fare una union


SELECT MAX(id),'query1' as tabella FROM post
union all
SELECT matr_dipendente,'query2' FROM inserisce WHERE id_post = x
union all
SELECT nome,'query3' FROM dipendente WHERE matricola='pippo'


tramite il campo fittizio avresti cosė anche un'indicazione in merito a quale tabella fa riferimento il tuo record.

shimo-ne
18-06-2008, 16:22
Potresti provare con le join visto che poi tu da codice fai quello alla fine:

select post.id, inserisce.matr_dipendente, dipendente.nome
from post inner join (
inserisce inner join dipendente
on matr_dipendente = matricola )
on id = id_post
order by id desc limit 1;


Sto facendo un programma in java/jsf/jsp utilizzando mysql come dbms; ora nel get di una variabile eseguo queste 3 query:
public String getNome_matr() {
sql ="SELECT MAX(id) FROM post";
ResultSet rs = sqlCommandBean.executeQuery(sql);
try{
rs.next();
this.id = rs.getInt("MAX(id)");
}catch (Exception e){}
sql="SELECT matr_dipendente FROM inserisce WHERE id_post='"+this.id+"'";
ResultSet rs1 = sqlCommandBean.executeQuery(sql);
try{
rs1.next();
matr_post = rs1.getInt("matr_dipendente");
}catch (Exception e){}
sql ="SELECT nome FROM dipendente WHERE matricola='"+matr_post+"'";
ResultSet rs2 = sqlCommandBean.executeQuery(sql);
try{
rs2.next();
nome_matr= rs2.getString("nome");
}catch(Exception e){}
return nome_matr;
}


Come posso concentrare queste 3 query per avere solo 1 resultset?
Avendo delle buone prestazioni magari

shinya
18-06-2008, 16:44
Io farei una roba cosi (sempre che abbia capito cosa vuoi ottenere):


select i.matr_dipendente, d.nome
from dipendente d join inserisce i on d.matricola = i.matr_dipendente
where i.id_post = (select max(id) from post)


edit: Chiudi quei ResultSet e non scrivere orrori come quel "catch(Exception e){}"!!

Redvex
18-06-2008, 16:46
Io farei una roba cosi (sempre che abbia capito cosa vuoi ottenere):


select i.matr_dipendente, d.nome
from dipendente d join inserisce i on d.matricola = i.matr_dipendente
where i.matr_dipendente = (select max(id)
from post)


Grazie a tutti risolto :)