0
My code returns all users, checking if they paid the last month, however I connect two tables, one of all customers with the registered payments table, I want to print on jTable
even if you have no payment records, however left join
seems to be playing the same role as inner join
,
follows the piece of code:
public void preencherTabela(){
ArrayList dados = new ArrayList();
String [] Colunas = new String[]{"CPF", "NOME", "DATA", "PAGO"};
try{
Connection con = Conexao.getConexao();
Statement stmt = con.createStatement();
ResultSet RS = stmt.executeQuery("Select cadastrarpac.nome, cadastrarpac.cpf, max(pagamento.data) from cadastrarpac "
+ "left join pagamento on (cadastrarpac.IdPac=pagamento.id_pac)");
while(RS.next()){
String nome = RS.getString("nome");
String cpf = RS.getString("Cpf");
String data = RS.getString("max(pagamento.data)");
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(data);
int data2 = date.getMonth()+1;
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
int data3 = now.getMonth()+1;
boolean pago;
if (data2==data3){
pago=true;
if (data.isEmpty()){
pago=false;
}
}else{
pago=false;
}
dados.add(new Object[]{cpf, nome, data, pago});
}
}catch(Exception e){
}
ModeloTabela modelo = new ModeloTabela(dados, Colunas);
table.setModel(modelo);
table.getColumnModel().getColumn(0).setPreferredWidth(80);
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setPreferredWidth(176);
table.getColumnModel().getColumn(1).setResizable(false);
table.getColumnModel().getColumn(2).setPreferredWidth(75);
table.getColumnModel().getColumn(2).setResizable(false);
table.getColumnModel().getColumn(3).setPreferredWidth(80);
table.getColumnModel().getColumn(3).setResizable(false);
table.getTableHeader().setReorderingAllowed(false);
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
Tabela pagamento:
id_pag int not null primary key, //id do registro de pagamento
id_pac int not null //id do paciente(cliente)
data varchar(80) not null //data do pagamento
insert into pagamento (id_pac, data) values (1, 20/04/2016)
insert into pagamento (id_pac, data) values (2, 21/04/2016)
Tabela cadastrarpac:
IdPac int not null primary key, //id do paciente
Cpf varchar(12) not null, //cpf
Nome varchar(80) not null //nome
insert into cadastrarpac (Cpf, Nome) values (088.758.960-0, Matheus) //id 1
insert into cadastrarpac (Cpf, Nome) values (089.858.966-0, Miriam) //id 2
Any help is welcome, Thank you
Experiemente withdraw the max of max(payment.date) and perform the consultation again.
– Duque
If you do this no results come out, nor those who have record of payment
– Matheus Lopes
Very strange, your left table is the cadastrarpac, so you should return something if you don’t have it in the payment. Do another test remove parentheses and leave as follows: payment.id_pac = register.Idpa
– Duque
It continued giving the same result, very strange even, because I’m using the left and not the Internet... was to bring unrelated results...
– Matheus Lopes
Edit the question and put the structure of the two tables with only 2 or 3 fields for me to test here.
– Duque
How exactly do you want it? the code of the tables or just what they are formed?
– Matheus Lopes
I wanted the code and the data that forms it. But the code can be well summarized and the information too.
– Duque
All right, that’ll do?
– Matheus Lopes
This max() is screwing everything up ....
– mau humor
No LEFT JOIN with aggregation. Try to get this maximum with a subquery. http://stackoverflow.com/questions/26928246/how-to-user-aggregate-function-with-left-join-mysql
– mau humor
Or maybe a UNION as a query that seeks customers without payment.
– mau humor
I tested here, the problem is in MAX, with it the query only returns 1 result, because the other has no record, so MAX returns null, already in the other record, has a value, then it is returned. Take out the MAX and you’ll get it
– Duque
Without max, returned 2 results.
– Duque
Is that I need the max to get the last recorded date by each user, as I could do it?
– Matheus Lopes
Make a registration without associating in payment, without max will continue to bring only those who have payment....
– Matheus Lopes
Well, it returns right null value? that’s exactly what I want, but I wanted it to return showing this null, there’s no way?
– Matheus Lopes
You will have to use a SUB query to do this. You cannot return the null
– Duque