简体   繁体   中英

Delete records from table A where field x in Table B = y

I have the following query I have been using and it works well enough for displaying the records I want to delete in phpMyAdmin:

SELECT zen_customers_basket.*
FROM zen_customers_basket, zen_products
WHERE zen_customers_basket.products_id = zen_products.products_id
            AND zen_products.products_availability = 'out of stock'

I then check all the records and then delete them. It's fairly quick and painless, but I need a query that will automatically delete these records so I can automate it with some other queries that are scheduled to run once per hour. Any and all help would be appreciated. I know how to delete records but I'm not sure of the proper syntax here since I'm selecting things from two different tables and I don't want to screw anything up.

DELETE zen_customers_basket
FROM zen_customers_basket, zen_products
WHERE zen_customers_basket.products_id =          zen_products.products_id
        AND zen_products.products_availability =    'out of stock'
    DELETE FROM Table  WHERE EXISTS (SELECT zen_customers_basket.*
FROM zen_customers_basket, zen_products
WHERE zen_customers_basket.products_id = zen_products.products_id
            AND zen_products.products_availability = 'out of stock')

https://stackoverflow.com/questions/8019957/delete-from-where-i n

Delete using same conditions as per your query. Not sure the deleting based on hour. Please clarify.

DELETE FROM zen_customers_basket
   INNER JOIN zen_products
   ON zen_customers_basket.products_id = zen_products.products_id
   WHERE zen_products.products_availability = 'out of stock'

Reference: T-SQL: Selecting rows to delete via joins

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