Results 1 to 3 of 3

Thread: Insert values from one table to another table

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    241

    Thumbs up Insert values from one table to another table

    Hi,

    I have two tables. Table Name 1 : UserPermission Table Name 2: UserFeaturetype.
    Code:
     UserPermission                                                                        UserFeaturetype
    
    ============                                                                        =============
    
     UserId  Usertypeid  featureoperationid                                         Usertypeid             featureoperationid
    
    60            3                   5                                                             3                               1
    
    60            3                   6                                                             3                               3
    
    60            3                  7                                                              3                               8
    
                                                                                                       3                               5
    
                                                                                                       3                               6
    
                                                                                                       3                               7


    What is my query that in UserPermission table i need to insert UserFeaturetype table values without already inserted featureoperationid that is 5,6,7 is already inserted in Table 1. I need to avoid duplication that is need to insert 1,3,8.

    My output could be like that is
    Code:
     60      3       5
    
    60       3       6
    
    60      3        7
    
    60      3       1
    
    60      3        3
    
    60      3       8


    Give me suggestion. Hope's your reply soon.



    Thanks
    Last edited by si_the_geek; Jan 27th, 2010 at 09:19 AM. Reason: added code tags for readability (even tho the are technically wrong)
    Failing to plan is Planning to fail

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Insert values from one table to another table

    This is one way you could do it:
    Code:
    INSERT INTO UserPermission (UserId, Usertypeid, featureoperationid)
      SELECT 60, UF.Usertypeid, UF.featureoperationid
      FROM UserFeaturetype UF
           LEFT JOIN UserPermission UP ON (UF.Usertypeid = UP.Usertypeid AND UF.featureoperationid = UP.featureoperationid)
      WHERE UP.Usertypeid Is Null

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Insert values from one table to another table

    Si's query is right but another way (and one I personally find a bit more readable) is to use a NOT EXISTS clause:-
    Code:
    INSERT INTO UserPermission (UserId, Usertypeid, featureoperationid)
      SELECT 60, UF.Usertypeid, UF.featureoperationid
      FROM UserFeaturetype UF
      WHERE NOT EXISTS
        (SELECT * FROM UserPermission UP
        WHERE UF.Usertypeid = UP.Usertypeid AND UF.featureoperationid = UP.featureoperationid)
    The performance should be roughly similar.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

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



Click Here to Expand Forum to Full Width