SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Brazil
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Query With Join

    Hi,

    I'm using MS SQL and have 2 tables.

    table1 has 384 records
    table2 has 252 records (that are duplicates of 252 of table1's rows)

    I can perform the join to tell me there are 252 rows that match:

    SELECT COUNT(*) AS Expr1
    FROM table1 INNER JOIN
    table2 ON table1.part_number = table2.part_number

    How do I format the query so I can DELETE the 252 rows of table1 so I'm left with only the 132 rows that aren't contained in table2?

    Thanks

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    delete
      from table1 
     where part_number
        in ( select part_number
               from table2 )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Brazil
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    Code:
    delete
      from table1 
     where part_number
        in ( select part_number
               from table2 )
    That seems incredibly simple after spending a couple of hours on it....

    thanks very much.

  4. #4
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Brazil
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, one other question...

    How do I then write a query to UPDATE a third table...

    Code:
    UPDATE    table3
    SET      table3.qty = (SELECT     *
    		FROM   table3 INNER JOIN
                          table2 ON  table2.prod_num = table3.prod_num )
    Basically I want UPDATE the qty value of an existing table (table3) that contains the 252 records in table2...

    Hope that makes some sense??

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    update table3
       set qty = t2.qty
      from table3 t3
    inner
      join table2 t2
        on t3.prod_num = t2.prod_num
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •