Solved

how do I formulate this DELETE query?

Posted on 2012-04-11
10
336 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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 51

Expert Comment

by:HainKurt
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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