Jump to content

Delete from multiple tables


maxat

Recommended Posts

Hi, I am new in mysql and having problem with deleting rows from multiple tables.

I have 3 tables

1. table users
us_id <- primary key

2. table items

  od_id <- primary key
  pd_id <- primary key
  us_id <- primary key
  it_qty

3.table orders

od_id <- primary key


In 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.
Link to comment
https://forums.phpfreaks.com/topic/15234-delete-from-multiple-tables/
Share on other sites

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]
DELETE
users,items,orders
FROM
users
LEFT JOIN
items
ON users.us_id=items.us_id
LEFT JOIN
orders
ON items.od_id=orders.od_id
WHERE users.us_id = $usrID
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.