SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select and insert value from other table

    Hi!
    I have a problem:
    i have this table Kontakt with following columns:
    KONTAKTPERS_NAMN,KONTAKTPERS_TELE,KONTAKTPERS_MOBIL,KONTAKTPERS_EMAIL,ADRESS_ID

    now i want to insert a new column called PLACE and insert values from an other table called ADRESS witch have following columns:
    ADRESS_ID,PLACE


    So i need to select the place value from Adress were ADRESS_ID is like ADRESS_ID in Kontakt

    Hope you guys understood what i try to do :-)

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    So you want to perform a JOIN. Do you still want to send back the KONTAKTPERS_NAMN,KONTAKTPERS_TELE,KONTAKTPERS_MOBIL,KONTAKTPERS_EMAIL, even if there is no address in the ADDRESS table? As that will determine if you use a LEFT or RIGHT JOIN.

    If you search for JOIN and the database you are using (MSSQL, MySQL, Postgres, etc) you will quickly find examples on how to write your JOIN.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!
    i would make a new table called Contacts with following columns:
    KONTAKTPERS_NAMN,KONTAKTPERS_TELE,KONTAKTPERS_MOBIL,KONTAKTPERS_EMAIL,ADRESS_ID,PLACE

    But how can i insert the data from the other 2 tables?

    Should i use 2 queries in mysql?
    Insert * from kontakt were ADRESS_ID like ADRESS_ID

    crap i cant understand how to do this :-)

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Code:
    INSERT INTO Contacts 
    SELECT KONTAKTPERS_NAMN, KONTAKTPERS_TELE, KONTAKTPERS_MOBIL, KONTAKTPERS_EMAIL, k.ADRESS_ID, PLACE
      FROM Kontakt AS k LEFT JOIN ADDRESS AS a ON k.ADRESS_ID = a.ADRESS_ID


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
  •