两个MySQL表的复杂连接

时间:2012-10-29 20:38:56

标签: mysql sql database join

编辑:我想要的联接:

enter image description here

这是我目前的MySQL表格

表'primary'

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

和表'辅助'

+----------+------------+---------+-----------+
| 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       |
| ..       | ..         | ...     | ...       |
+----------+------------+---------+-----------+

可以在一个查询中组合: - 从主表的行单击中选择全部,从次表中选择全部来自weavenumber +特定列:

例如如果点击主表的第2行,选择全部,我也想从weavenumber和列'siderite'中选择所有。

Weavenumber列对于所有选择都是常见的,唯一的区别是第3,4,5列等,它们对应于主表的id。 可以像这样进行连接吗?

我想为每个条目主表制作一个xy图,而在二级表中,weavenumber是x-ax(水平),方解石,菱铁矿等是变量y-ax(垂直)。 / p>

现在我有了这个:

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);
        }

}

1 个答案:

答案 0 :(得分: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