我如何编写适当的内部联接查询

时间:2019-06-14 16:28:39

标签: mysql sql

我正在尝试使用内部联接语法在一个查询中合并两个表记录。

table one

Unique_ID | School | Paid | Balance | Date  |
+---------+--------+------------- --+-------+
 112     MaryGold   200     500    2/5/19 
 112     MaryGold   300     200    9/5/19 
 107      MyKids    100     500    9/5/19 
 107      MyKids    100     400    2/6/19


and table two

Unique_ID | School | amountperterm | Academic_Year | Term | c_Paid | c_Balance |
+---------+--------+---------------+---------------+------+--------+-----------+
 112     MaryGold       700          2019 / 2020     1     500        200
 107      MyKids        600          2019 / 2020     1     200        400


表1显示了每笔付款和余额的记录
表2显示了所有已付款项的总和余额的记录

我想要一个联接查询,其中表2中的学校记录将链接到表1中的所有付款记录。

我正在使用碧玉报告来打印所有学校记录及其各自的账单。
该代码适用于在Jtable中选择学校记录并按下print_button的情况。

int row = s_ThirdTerm.getSelectedRow();
            try{
            String table_click=(s_ThirdTerm.getModel().getValueAt(row,0).toString());    
            JasperDesign jd =JRXmlLoader.load("printOuts\\ucmasReciept.jrxml");
            String sgl="select school_info.Unique_ID,school_info.School,school_info.amountperterm,school_info.Academic_Year,school_info.Term,school_info.currentallpaid,school_info.currentbalance,school_info.currentdate,school_bill_archive.Paid,\n" +
            "school_bill_archive.Balance,school_bill_archive.Date from school_bill_archive INNER JOIN school_info on school_info.Term=school_bill_archive.Term and school_info.Unique_ID=school_bill_archive.Unique_ID and school_info.School='"+table_click+"'";
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(sgl);
            jd.setQuery(newQuery);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp = JasperFillManager.fillReport(jr,null,con);
            JasperViewer.viewReport(jp,false);

        }catch(Exception e){
            JOptionPane.showMessageDialog(null,e);

        }finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){
            }
            }

它在表2中显示所选学校,并在表1中显示其所有帐单支付记录。

但是我想要一种情况,我不会选择特定的学校,但是当我点击print_button时,碧玉报告应该打开,其中包含所有学校的记录及其各自的付款记录。

例如如果我在表2中有5所学校,那么在按print_button时,我应该在jasper报告中有5页。表2中的每一个页面都有学校的信息,表1中的相应付款。

这是我编写的代码,但它所做的只是显示一个页面,其中包含表2中第一行的记录以及表1中的所有记录。

            try{

            JasperDesign jd =JRXmlLoader.load("printOuts\\Reciept.jrxml");
            String sgl="select school_info.Unique_ID,school_info.School,school_info.amountperterm,school_info.Academic_Year,school_info.Term,school_info.currentallpaid,school_info.currentbalance,school_info.currentdate,school_bill_archive.Paid,\n" +
            "school_bill_archive.Balance,school_bill_archive.Date from school_bill_archive INNER JOIN school_info on school_info.Term=school_bill_archive.Term and school_info.Unique_ID=school_bill_archive.Unique_ID and school_info.School=school_bill_archive.School";
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(sgl);
            jd.setQuery(newQuery);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp = JasperFillManager.fillReport(jr,null,con);
            JasperViewer.viewReport(jp,false);

        }catch(Exception e){
            JOptionPane.showMessageDialog(null,e);

        }finally{
            try{
                rs.close();
                pst.close();
            }
            catch(Exception e){
            }
            }


1 个答案:

答案 0 :(得分:0)

如果要查看特定学校的所有付款,则需要按如下所示向查询添加过滤器-

SELECT
<columns>
FROM school_info si
INNER JOIN school_bill_archive sba
ON si.Unique_ID = sba.Unique_ID -- This should be the joining key
WHERE si.Unique_ID = XX -- Replace XX with desire Unique ID
AND si.term = '<Put Term Value>'-- You can also filter by particular term if required