Search code examples
mysqljoincontent-management-systemdelete-row

deleting all the rows joined with their page post


I need to delete the article's pages of a blog and their relative images, linked by a JOIN.

Tables of page are: id, label, title, body, slug, created, updated, images.id

Tables of images are: id, page_id, content, img_title, img_url, img_ftp_path, thumb_ftp_path, thumb_delete_path

I can delete images one by one but when I click on "delete page" I would like to clear all the table's page and every row of the relative image.

I am using this mysql command, but it doesn't works:

    DELETE pages.id, pages.label, pages.title, pages.body, pages.slug, pages.created, pages.updated, images.id, images.page_id, images.content, images.img_title, images.img_url, images.img_ftp_path, images.thumb_ftp_path, images.thumb_delete_path
    FROM pages
    LEFT JOIN images
    ON pages.id = images.page_id 
    WHERE id = :id

What's wrong?


Solution

  • http://dev.mysql.com/doc/refman/5.7/en/delete.html

    You don't specify fields, you delete rows from a table.

     DELETE p, i
        FROM pages p
        JOIN images i ON p.id = i.page_id 
        WHERE p.id = :id