maxat Posted July 21, 2006 Share Posted July 21, 2006 Hi, I am new in mysql and having problem with deleting rows from multiple tables.I have 3 tables1. table users us_id <- primary key2. table items od_id <- primary key pd_id <- primary key us_id <- primary key it_qty3.table orders od_id <- primary keyIn table "items" all are foreign keys from another tables except it_qty. The problem is when I delete user from table "user", I want delete all his records from "items" and "order" tables. My tables are in MyIsam. so far I couldn't get any valid query. Please help me with this query. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/ Share on other sites More sharing options...
designationlocutus Posted July 21, 2006 Share Posted July 21, 2006 Are you linking all of your tables together within the DELETE query?Please post your query contruct. Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61554 Share on other sites More sharing options...
maxat Posted July 21, 2006 Author Share Posted July 21, 2006 DELETE FROM users, items, orders WHERE users.us_id = items.us_id AND items.od_id = orders.od_id AND users.us_id = '$usrID'is there anything wrong? Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61556 Share on other sites More sharing options...
shoz Posted July 21, 2006 Share Posted July 21, 2006 Try the following[code]DELETEu,i,oFROMusers AS uLEFT JOINitems AS iON u.us_id=i.us_idLEFT JOINorders AS oON i.od_id=o.od_idWHERE u.us_id = $usrID[/code][url=http://dev.mysql.com/doc/refman/4.1/en/delete.html]Delete Syntax[/url] Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61629 Share on other sites More sharing options...
maxat Posted July 21, 2006 Author Share Posted July 21, 2006 Thanks shoz for reply. I have tried to run the query, but getting an error " Not unique table/alias: 'u' " Do I have to change my column names? Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61638 Share on other sites More sharing options...
shoz Posted July 21, 2006 Share Posted July 21, 2006 MYSQL 4.0 and MYSQL 4.1 or higher deal with aliases in multi-table deletes differently. To avoid problems try this query which doesn't use aliases[code]DELETEusers,items,ordersFROMusersLEFT JOINitemsON users.us_id=items.us_idLEFT JOINordersON items.od_id=orders.od_idWHERE users.us_id = $usrID[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61644 Share on other sites More sharing options...
maxat Posted July 21, 2006 Author Share Posted July 21, 2006 Thank you shoz very much. It is working now. I realy appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/#findComment-61670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.