Solved

how do I formulate this DELETE query?

Posted on 2012-04-11
10
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
yawkey13 earned 500 total points
ID: 37835144
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
ID: 37835149
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
ID: 37835196
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 6

Expert Comment

by:yawkey13
ID: 37835283
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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 37835784
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
 

Author Comment

by:bitt3n
ID: 37844274
@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
ID: 37844295
@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
ID: 37856068
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
ID: 37857133
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
ID: 37857268
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

688 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