Delete all rows in a table based on another table
DELETE Table1
FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID
How to delete records in one table based on the values in another table?
delete
from table1
where cm_id IN (select um_id from table2 where order_num between 518 and 520)
SQL Delete Rows Based on Another Table
I think this is what you want:
DELETE FROM `table1`
WHERE `group` in (SELECT DISTINCT `group` FROM `table2`)
Delete rows depending on values from another table
You were very close. I suggest you use NOT IN
instead of IN
:
DELETE FROM Customer
WHERE CustID = $1 AND
CustID NOT IN (SELECT DISTINCT CustID
FROM Order
WHERE Status = 'A');
I'm guessing here that Status = 'A'
means "active order". If it's something else change the code appropriately.
How do I delete rows in one table where the ID matches another table row where a field is a certain value?
try apply join
permissions
and user
and where status = 'T'
Example:
DELETE p
FROM permissions p
INNER JOIN users u
ON p.user_id=u.id
WHERE u.status = 'T'
How to delete records in one table based on the values in another table
Not quite clear about the question. Sounds like: remove all wish_list
entries for books that have quantity 0 in the book
table?
DELETE FROM wish_list
WHERE isbn IN ( SELECT isbn
FROM book
WHERE quantity = 0 );
Or the update:
UPDATE wish_list
SET quantity = -1
WHERE isbn IN ( SELECT isbn
FROM book
WHERE quantity = 0 );
Update: Deleting duplicates from wish_list
(regardless of quantity). Please test, I haven't:
DELETE FROM wish_list
WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM wish_list GROUP BY isbn);
Related Topics
SQL Server Script to Create a New User
How Different Is Postgresql to MySQL
Changing Precision of Numeric Column in Oracle
How to Flip a Bit in SQL Server
SQL Server Management Studio 2012 - Export All Tables of Database as CSV
Sql: How to to Sum Two Values from Different Tables
Cumulating Value of Current Row + Sum of Previous Rows
Oracle SQL: Use Sequence in Insert with Select Statement
Use Row Values as Columns in Postgresql
Is a One Column Table Good Design
MySQL Auto-Store Datetime for Each Row
When to Use an Auto-Incremented Primary Key and When Not To
Convert Integer to Text in SQLite's Select Query
Postgresql: Foreign Key/On Delete Cascade
SQL Delete Rows Based on Another Table
SQL to Query Text in Access with an Apostrophe in It
How to Get Previous Business Day in a Week with That of Current Business Day Using SQL Server