How to merge MySQL tables?

Discussion in 'MySQL' started by caslab, Jun 23, 2010.

  1. #1
    I have 2 MySQL tables that I want to merge together where a CAS number in one matches the other. Each table has some overlapping info, some unique.

    I want to merge table1 into table2 where the CAS Number matches, but only UPDATE the fields that are empty (I don't want to replace info already there)

    If CAS number does not exist in table2, I want to INSERT this info (I'm assuming the insert will autonumber my IDs as normal, which is what I want).

    Anyone know how to accomplish this?
     
    caslab, Jun 23, 2010 IP
  2. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #2
    Something like this:

     
    georgiivanov, Jun 23, 2010 IP
  3. 211classclown

    211classclown Active Member

    Messages:
    140
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I was looking to do the same... ill be sure to try this method :)

    thanks
     
    211classclown, Jun 23, 2010 IP
  4. caslab

    caslab Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you, that code works.

    2 questions:

    AND t1.col1 IS NULL OR t1.col1 = ''
    Code (markup):
    1. Is the above redundant? Or does NULL not include '' and '' does not include NULL?

    2. Can the update part of the code be modified to update multiple fields at once? As is, I would have to modify and run the SQL code 30 or more times to update all the fields and surely there's an easier way? (they all use the same common id)

    Thanks!
     
    caslab, Jun 23, 2010 IP
  5. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #5
    1) NULL is ... just NULL. You can't compare it, that is why the expression is col1 IS NOT NULL. You cant check for col1 = NULL or col1 != NULL. :)

    2) You can write something like this:
    The real problem here is with the WHERE clause. This way you append more conditions using OR.
     
    georgiivanov, Jun 23, 2010 IP
  6. caslab

    caslab Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So, the part: t1.col1 = '' ...will not return NULL values, then? So, anytime I query MySQL not wanting blank/empty records, I need to do both: t1.col1 IS NULL OR t1.col1 = ''
     
    caslab, Jul 6, 2010 IP
  7. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #7
    Dude, if col1 is PRIMARY KEY - it can't be NULL :) So the WHERE clause is not needed.
     
    georgiivanov, Jul 6, 2010 IP
  8. Alexandros1

    Alexandros1 Peon

    Messages:
    332
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The patch is untested, but should work on a new Drupal install. If your mysql install supports the MERGE engine, then you will have the two tables created for watchdog and accesslog log events. Inserts will go into the _insert table but will be selected from the merged log tables.
     
    Alexandros1, Jul 7, 2010 IP