查询从Access中的多个表中删除

时间:2014-06-19 09:32:02

标签: java ms-access jdbc ms-access-2007 ms-access-2010

我想从Access数据库中的多个表中删除一行。

这是我试图在我的项目中使用的代码,但是我收到了错误。

PreparedStatement ps = con.prepareStatement("DELETE FROM 'customer_details' , 'papers', 'magzines' WHERE 'customer_id' = ? ");   
ps.setString(1,tx1.getText());
int string = ps.executeUpdate();

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我只是尝试了以下内容,它对我有用:

String sql = 
        "DELETE t1.*, t2.*, t3.* " +
        "FROM " +
            "(" +
                "Table1 AS t1 " +
                "INNER JOIN " +
                "Table2 AS t2 " +
                    "ON t2.ID=t1.ID " +
            ")" +
            "INNER JOIN " +
            "Table3 AS t3 " +
                "ON t3.ID=t2.ID " +
        "WHERE t1.ID=?";
ps = con.prepareStatement(sql);
ps.setInt(1, 4);  // delete where ID=4
int n = ps.executeUpdate();

所以在你的情况下尝试这样的事情:

PreparedStatement ps = con.prepareStatement(
        "DELETE c.*, p.*, m.* " +
        "FROM " +
            "(" +
                "customer_details AS c " +
                "INNER JOIN " +
                "papers AS p " +
                    "ON p.customer_id=c.customer_id " +
            ")" +
            "INNER JOIN " +
            "magzines AS m " +
                "ON m.customer_id=p.customer_id " +
        "WHERE c.customer_id=?");   
ps.setString(1,tx1.getText());
int n = ps.executeUpdate();