Solved

how do I formulate this DELETE query?

Posted on 2012-04-11
10
337 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP - AJAX and MySQL it works only if the value is a number 12 90
PHP: Insert Data into MySQL 5 61
Complex MySQL Query 2 38
Combining Queries 7 38
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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