Advertisement

03.30.2008 at 04:49PM PDT, ID: 23281300
[x]
Attachment Details

MySQL Subquery Syntax?

Asked by iSac1984 in MySQL, SQL Query Syntax

Tags: MySQL Subqueries

In my database there are three related tables, call them T1, T2, and T3. Each has two columns, I and J, that form the primary key. Each entry is, in effect, an ordered pair, (x, y), and due to the primary key, there can be no duplicate pairs within each table (ie (2, 3) and (2,4) can be in the same table, but not (2, 3) and (2,3).

However, it is also important that there be no duplicate entries from table to table. Specifically, there should be no pairs in T2 that are already in T1, and no pairs in T3 that are in T1 or T2.

I was going to write a function to do this using PHP, but I realized I could use a subquery to do it as well. I've written the nested query, and tried it on a test DB, but since the information in the tables is fairly critical, I'd like to make sure it's not a fluke that'll blow up later.

If someone could let me know if this query is safe or not, I'd appreciate it. ThanksStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
//this first one is to delete any entry (i,j) from T3 that is already in 
//T2 or T1
delete from T3
where I = whatever
and (J in (select J from T2 where I = whatever) or
    (J in (select J from T1 where I = whatever));
 
//this is to delete any entry (i,j) from T2 that is already in T1
delete from T2
where I = whatever
and J in (select J from T1 where I = whatever)
 
Loading Advertisement...
 
[+][-]03.30.2008 at 07:27PM PDT, ID: 21242823

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MySQL, SQL Query Syntax
Tags: MySQL Subqueries
Sign Up Now!
Solution Provided By: Lowfatspread
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628