Solved

how do I formulate this DELETE query?

Posted on 2012-04-11
10
333 Views
Last Modified: 2012-04-17
I have two tables: products(product_id,date) and sex(product_id,sex).

I want to DELETE from the products table all products having a product_id for which a row exists in the sex table with this product_id and sex=1.

I also wish to DELETE from the sex table all rows containing a product_id contained in one of the rows I am deleting from the products table.

(This may delete more rows from the sex table than the products table, because a given product_id might appear more than once in the sex table (once with sex=1, once with sex=0).)
0
Comment
Question by:bitt3n
  • 6
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
yawkey13 earned 500 total points
Comment Utility
DECLARE recordsToDelete AS TABLE(
int product_id
)

--identify records that exist in products and sex and where sex = 1
INSERT INTO recordsToDelete( product_id )
SELECT product_id
FROM products p
   JOIN sex s
     ON p.product_id = s.product_id
WHERE s.sex = 1;

--SELECT * FROM recordsToDelete

DELETE FROM products
WHERE product_id IN ( SELECT product_id FROM recordsToDelete );

DELETE FROM sex
WHERE product_id IN ( SELECT product_id FROM recordsToDelete );
0
 
LVL 6

Expert Comment

by:yawkey13
Comment Utility
Check the results of the SELECT statement prior to running the delete to make sure I properly understood the question.
0
 

Author Comment

by:bitt3n
Comment Utility
that makes sense intuitively, but I am getting a syntax error in phpMyAdmin. I looked up the syntax for DECLARE and it says  at "DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements."

I then tried

BEGIN DECLARE recordsToDelete AS TABLE(
int product_id
);

INSERT INTO recordsToDelete( product_id )
SELECT product_id 
FROM products p
   JOIN sex s
     ON p.product_id = s.product_id
WHERE s.sex = 1;

SELECT * FROM recordsToDelete;

END

Open in new window


I gather I am still screwing up the syntax?
0
 
LVL 6

Expert Comment

by:yawkey13
Comment Utility
my bad.  put an '@' symbol in front of everywhere I have recordsToDelete.   So it would be @recordsToDelete.  You shouldn't need the begin/end.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
maybe this:

create table temptable (prdid integer);
insert into temptable (prdid)  select prdid from sextable s where p.product_id=s.product_id and s.sex=1

delete from products where prdid in (select prdid from temptable);
delete from sextable where prdid in (select prdid from temptable);

drop table temptable;
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:bitt3n
Comment Utility
@yawkey13

in phpMyAdmin I tried

DECLARE @recordsToDelete AS TABLE(
int product_id
)

INSERT INTO @recordsToDelete( product_id )
SELECT product_id 
FROM products p
   JOIN sex s
     ON p.product_id = s.product_id
WHERE s.sex = 1;

SELECT * FROM @recordsToDelete

Open in new window


but I still get

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @recordsToDelete AS TABLE(
int product_id
)

INSERT INTO @recordsToD' at line 1 

Open in new window

0
 

Author Comment

by:bitt3n
Comment Utility
@HainKurt

would it be preferable to declare a temporary table rather than create a regular table and drop it afterward? I'm not sure what the difference is, although it seems like a temporary table would make more sense.
0
 

Author Comment

by:bitt3n
Comment Utility
I'm still having this problem. Is there any additional information I could provide that might help determine why I am getting the syntax error?
0
 

Author Comment

by:bitt3n
Comment Utility
even the DECLARE statement produces a syntax error. it says here http://dev.mysql.com/doc/refman/5.0/en/declare.html that a BEGIN/END sequence is required for DECLARE, but I still can't get it to work. Any advice would be greatly appreciated.
0
 

Author Comment

by:bitt3n
Comment Utility
solved via

CREATE TEMPORARY TABLE recordsToDelete(
product_id INT
);# MySQL returned an empty result set (i.e. zero rows).
INSERT INTO recordsToDelete( product_id ) 
SELECT p.product_id
FROM products p
JOIN sex s ON p.product_id = s.product_id
WHERE s.sex =1;# Affected rows: 275
SELECT * 
FROM recordsToDelete;

Open in new window


DECLARE would be the syntax for MS SQL I think
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now