Solved

how do I formulate this DELETE query?

Posted on 2012-04-11
10
338 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 53

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to insert record into a table 2 55
Mysql Left Join Case 10 114
Complex MySQL Query 2 46
mysql workbecn having problems to export tables to cvs 4 23
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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