Mysql cross-Table Deletion

Source: Internet
Author: User

A few days before mysql cross-Table deletion, I wrote a summary of Mysql cross-Table update. Today we will look at cross-Table deletion. After www.2cto.com and Mysql4.0, mysql began to support cross-Table delete. Mysql can delete multiple table records in an SQL statement at the same time, or delete records in a table based on the relationship between multiple tables. Suppose we have two tables: The Product table and the ProductPrice table. The former has the basic information about the Product, and the latter has the price of the Product. The first cross-Table deletion method is to delete multiple tables without the need for join operations. During the DELETE operation, multiple tables are separated by commas (,). The following SQL statement is used: delete p. *, pp. * FROM product p, productPrice pp WHERE p. productId = pp. productId AND p. created <'1970-01-01 'the second method of cross-Table deletion is to use inner join to specify the association between the two tables in join. The following SQL statement is used: DELETE p. *, pp. * FROM product p inner join productPrice pp ON p. productId = pp. productId WHERE p. created <'1970-01-01 'Note: you do not have to delete data from all tables for cross-Table deletion. The preceding SQL statement table deletes data from both the Product and ProductPrice tables, but you can specify DELETE product. * only records in the product table are deleted, and records in the ProductPrice table are not processed. You can also use left join for cross-Table deletion. For example, we want to delete all the Product table records that are not recorded in the ProductPrice table. The following SQL statement: DELETE p. * FROM product p left join productPrice pp ON p. productId = pp. productId WHERE pp. productId is null cross-Table deletion is very useful. Use it as needed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.