简体   繁体   中英

MySQL Deletion Query, Multiple Inner Joins

I want to delete an "order" from my database using the primary key, orderID. The orderID is a foreign key in orderItem, OrderItem has a foreign key in foodItem called foodItemID, foodItem has a foreign key in menu called menuID.

Below is my query:

            "DELETE FROM orders, orderitem, fooditem, menu " +
            "USING orders " +
            "INNER JOIN orderitem " +
            "WHERE orderID = " + "'" + orderID + "' " + " AND orders.orderID = orderItem.orderID " +
                    "INNER JOIN foodItem " +
                    "WHERE orderItem.foodItemID = foodItemID.foodItemID " +
                    "INNER JOIN Menu " +
                    "WHERE foodItem.menuID = menu.menuID";

I am coding in JAVAFX.. The error I receive is:

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN foodItem WHERE orderItem.foodItemID = foodItemID.foodItemID INNER JOI' at line 1

You don't join WHERE , you join ON something. So

"DELETE FROM orders, orderitem, fooditem, menu " +
"USING orders " +
"INNER JOIN orderitem " +
"ON orderID = " + "'" + orderID + "' " + " AND orders.orderID = orderItem.orderID " +
"INNER JOIN foodItem " +
"ON orderItem.foodItemID = foodItemID.foodItemID " +
"ON JOIN Menu " +
"ON foodItem.menuID = menu.menuID";

Also, this part

"ON orderID = " + "'" + orderID + "' " + "

Is a SQL Injection waiting to happen. You should use prepared statements instead. How you use them depends on what library/framework you're using to connect to the db (which you didn't specify).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM