Search code examples
mysqlpython-3.xbetter-sqlite3

How to inner join two tables?


To extend a column (col_F) into the final output (File3) by comparing File 1 and File2.

Col_A has possessed a relationship between File1 and File2. I have to use this relationship and omit values in the col_F into File3.

For an example:

File1
col_A       col_F                          
 A1      F1
 A2      F2
 A5      F5
 AZ      FZ
 AX      FX
 A#      F#
 A2      F2


File2  
col_A  col_B      col_C   col_D
  A1    B        C       D
  A2    B        C       D
  A5    B        C       D
  AZ    B        C       D
  AX    B        C       D
  A#    B        C       D
  A2    B        C       D


File3_Final output (Query result) 
col_A  col_B    col_C   col_D   col_F
A1      B        C        D      F1
A2      B        C        D      F2
A5      B        C        D      F5
A       B        C        D      FZ
AX      B        C        D      FX
A#      B        C        D      F#
A2      B        C        D      F2


NOTE: All the Tables are saved in .csv format

Solution

  • You say you want solutions in Python, MySQL or MongoDB. But you've tagged the question with "perl". So here's a Perl solution.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %file1 = get_file1();
    
    open my $fh2, '<', 'File2' or die "File 2: $!\n";
    
    chomp(my $header = <$fh2>);
    print "$header\tcol_F\n";
    
    while (<$fh2>) {
      chomp;
      my $colA = (split ' ')[0];
      print "$_\t$file1{$colA}\n";
    }
    
    sub get_file1 {
      my %hash;
    
      open my $fh1, '<', 'File1' or die "File 1: $!\n";
    
      <$fh1>; # skip headers
      while (<$fh1>) {
        chomp;
        my ($key, $val) = split ' ';
        $hash{$key} = $val;
      }
    
      return %hash;
    }