Results 1 to 3 of 3

Thread: Delete the entries of Table 1 according to criterion of other table

  1. #1
    Join Date
    Nov 2008
    Posts
    1,001

    Delete the entries of Table 1 according to criterion of other table

    I need to extract part of a MySQL database to be in line to make it accessible through a website. To make it lighter and also for reasons of security and simplicity for the future, I have a "cleaning" of the database and transfer all the entries that do not interest me or that I do not want to leave rested in row.

    To do this, I have 2 major complaints to remove entries from a table based on criteria in another table.

    My database is structured as follows:

    Code:
    Table (field names) 
    
    Table1 (id, name, type) 
    Table1bis (id, info1, Info2) 
    
    Table2 (id, Email1, email2) 
    Table2bis (id, module, date)
    The link between TableN and TableNbis is done by the ID.

    The first application that I have is to remove all the entries in Table1 why info1 fields of Table1bis is empty (when I say empty, it is not 'NULL' but a field that appears white).

    The second request is to delete all entries in the Table2 fields for which the module is 'Users' for example.

    I did a lot of research, I saw with formulas and DELETE FROM INNER JOIN, but do not seem to work. I also saw queries with SELECT and DELETE FROM IN () but my limited programming skills do not allow me to build them correctly

    What do you think would be the best option? Would someone kindly give me some examples of queries which I need?

  2. #2
    Join Date
    Feb 2008
    Posts
    194

    Re: Delete the entries of Table 1 according to criterion of other table

    Use the method provided to specify a reference table, without the risk of touch.

    DELETE FROM [ONLY] table [[AS] alias]
    [USING usinglist]
    [WHERE condition]
    [RETURNING * | output_expression [AS output_name] [, ...]]

    usinglist
    A list of table expressions, allowing columns from other tables to appear in the WHERE condition. This is similar to the list of tables that can be specified in the FROM clause of a SELECT statement, for example, an alias for the table name can be specified. Do not repeat the target table in the usinglist, unless you wish to set up a self-join.

    Code:
    DELETE FROM table1 
    USING table1bis 
    WHERE table1.id = table1bis.id 
    AND   table1bis.info =''
    and 
    DELETE FROM table2 
    USING table2bis 
    WHERE table2.id = table2bis.id 
    AND table2bis.module = 'Users'
    Note to avoid capital letters in the name of your tables is a good habit

  3. #3
    Join Date
    May 2008
    Posts
    3,971

    Re: Delete the entries of Table 1 according to criterion of other table

    The syntax that I used would be:

    DELETE FROM TABLE1 T1
    INNER JOIN TABLE1BIS T1B ON T1B.id = T1.id
    WHERE T1B.info =''

    If it does not pass, you can try:

    DELETE FROM TABLE1
    WHERE id IN (
    SELECT id FROM TABLE1BIS
    WHERE TABLE1BIS.info1 =''
    )

    In all cases, prior to running your DELETE, I advice you to do a SELECT * in place to validate that the selected lines are good. Once it is validated, you initiate a transaction (on SQL Server is BEGIN TRAN name_of_the_transaction), then execute your DELETE request, and if the number of rows affected is what you expect with your SELECT * to that valid transaction with a COMMIT TRAN name_of_the_transaction.

    I forgot, attention to integrity constraints before doing the DELETE, it will be a problem if you have any data to your table TABLE1 (this will delete all data related to these lines in the linked tables before can clean the table TABLE1).

Similar Threads

  1. Replies: 5
    Last Post: 24-01-2012, 05:02 PM
  2. Replies: 5
    Last Post: 27-08-2011, 10:53 AM
  3. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 02:01 PM
  4. How to Remove Duplicate Entries from Table ?
    By Faakhir in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 12:58 PM
  5. What is the difference between delete table & truncate table?
    By Swati in forum Software Development
    Replies: 4
    Last Post: 13-02-2009, 05:24 PM

Tags for this Thread

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
  •  
Page generated in 1,714,062,175.82146 seconds with 17 queries