根据行将数据从一列插入另一个MYSQL

时间:2016-02-23 10:33:31

标签: php mysql sql

我是MYSQL查询的新手,所以我在努力解决这个问题。

我有两张桌子

Table 1 

  id       phone1  phone2   name     ...
  1        123      456
  3        234      567
  7        345      678

Table 2

  id        p1        p2    age     ...
  1        1123      2456
  7        1345      2678
  3        1234      2567

两个表的ID相同。两个表都有许多其他行。

我想将表2中的数据复制到表1,以便id保持不变。

所以输出应该是

Table 1 
  id       phone1  phone2   name
  1        1123      2456
  3        1234      2567
  7        1345      2678

3 个答案:

答案 0 :(得分:1)

已经回答:

stackoverflow

在你的情况下:

UPDATE table1 t1
    INNER JOIN table2 t2 ON t2.id = t1.id
    SET t1.phone1 = t2.p1,
    t1.phone2 = t2.p2;

答案 1 :(得分:1)

试试这个。

Function Get-IniFile ($file)       # Based on "https://stackoverflow.com/a/422529"
 {
    $ini = [ordered]@{}

    # Create a default section if none exist in the file. Like a java prop file.
    $section = "NO_SECTION"
    $ini[$section] = [ordered]@{}

    switch -regex -file $file 
    {    
        "^\[(.+)\]$" 
        {
            $section = $matches[1].Trim()
            $ini[$section] = [ordered]@{}
        }

        "^\s*(.+?)\s*=\s*(.*)" 
        {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value.Trim()
        }

        default
        {
            $ini[$section]["<$("{0:d4}" -f $CommentCount++)>"] = $_
        }
    }

    $ini
}

Function Set-IniFile ($iniObject, $Path, $PrintNoSection=$false, $PreserveNonData=$true)
{                                  # Based on "http://www.out-web.net/?p=109"
    $Content = @()
    ForEach ($Category in $iniObject.Keys)
    {
        if ( ($Category -notlike 'NO_SECTION') -or $PrintNoSection )
        {
            # Put a newline before category as seperator, only if there is none 
            $seperator = if ($Content[$Content.Count - 1] -eq "") {} else { "`n" }

            $Content += $seperator + "[$Category]";
        }

        ForEach ($Key in $iniObject.$Category.Keys)
        {           
            if ( $Key.StartsWith('<') )
            {
                if ($PreserveNonData)
                    {
                        $Content += $iniObject.$Category.$Key
                    }
            }
            else
            {
                $Content += "$Key = " + $iniObject.$Category.$Key
            }
        }
    }

    $Content | Set-Content $Path -Force
}


### EXAMPLE
##
## $iniObj = Get-IniFile 'c:\myfile.ini'
##
## $iniObj.existingCategory1.exisitingKey = 'value0'
## $iniObj['newCategory'] = @{
##   'newKey1' = 'value1';
##   'newKey2' = 'value2'
##   }
## $iniObj.existingCategory1.insert(0, 'keyAtFirstPlace', 'value3')
## $iniObj.remove('existingCategory2')
##
## Set-IniFile $iniObj 'c:\myNewfile.ini' -PreserveNonData $false
##

答案 2 :(得分:1)

UPDATE Table1 tab1
    INNER JOIN Table2 tab2 ON tab2.id = tab1.id
    SET 
    tab1.Column2 = tab2.Column3;

选中此sqlfiddle