0

edit: The join that I want:

enter image description here

This is my current MySQL tables

table 'primary'

+----------+--------+---------+-----------+----------+
| id       | sample | mineral | formula   | link     |
+----------+--------+---------+-----------+----------+
| 1        | 5742   | calcite | CaCO3     | link..   |
| 2        | 3411   | siderite| Fe^2+^CO3 | link..   |
| .        | ....   | ........| ......... | ......   |
+----------+--------+---------+-----------+----------+

and table 'secondary'

+----------+------------+---------+-----------+
| id       | wavenumber | calcite | siderite  |
+----------+------------+---------+-----------+
| 1        | 1          | 256     | 434       |
| 2        | 3          | 254     | 312       |
| 3        | 5          | 251     | 424       |
| 4        | 7          | 257     | 544       |
| 5        | 9          | 258     | 345       |
| 6        | 11         | 257     | 386       |
| 7        | 13         | 326     | 314       |
| 8        | 15         | 456     | 337       |
| 9        | 17         | 236     | 243       |
| 10       | 19         | 246     | 514       |
| 11       | 21         | 254     | 662       |
| 12       | 23         | 258     | 248       |
| 13       | 25         | 230     | 374       |
| 14       | 27         | 224     | 359       |
| 15       | 29         | 216     | 442       |
| 16       | 31         | 236     | 344       |
| 17       | 33         | 247     | 343       |
| ..       | ..         | ...     | ...       |
+----------+------------+---------+-----------+

It is possible to combine in one query: - a select all from row click of primary table with select all from weavenumber + specific column from secondary table:

e.g. if is clicked on row 2 of primary table, select all and also I want to select all from weavenumber and column 'siderite'.

Weavenumber column it is common for all selections, the only difference is column 3, 4, 5, and so on, which corresponds to the id of primary table. It's possible to make a join like this?

I want to make a xy plot for every entry primary table, and in the secondary table, the weavenumber is the x-axe (horizontal) and calcite, siderite, etc. is the variable y-axe (vertical).

For now I have this:

private void primaryMouseClicked(java.awt.event.MouseEvent evt) {  

int row = primary.getSelectedRow();
int realIndex = primary.convertRowIndexToModel(row);
String Table_click = (primary.getModel().getValueAt(realIndex, 0).toString());

    try {
            String sql = "select * from primary where id='" + Table_click + "' ";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()) {


                String add1 = rs.getString("mineral");
                txt_mineral.setText(add1);
                String add2 = rs.getString("formula");
                txt_formula.setText(add2);
                String add3 = rs.getString("link");
                txt_link.setText(add3);

            }

        } catch (Exception e) {

            JOptionPane.showMessageDialog(null, e);
        }

}
4

1 回答 1

1

这是我提出的查询:

SELECT     Prime.*, wavenumber, 
           CASE 
             WHEN Prime.mineral = 'calcite' 
             THEN calcite 
             ELSE siderite 
           END AS mineralval 
FROM       Second 
RIGHT JOIN (SELECT * FROM Prime) AS Prime ON 1 = 1

http://sqlfiddle.com/#!2/ee3cf/8

于 2012-10-30T08:07:18.367 回答