简体   繁体   English

用来自另一个表的数据更新表

[英]update table with data from another table

I have two tables one tbl_demographics with coluimns [staffid, natid] and another tbl_nationality with columns [natid, description]. 我有两个表,一个tbl_demographics带有coluimns [staffid,natid],另一个tbl_nationality带有列[natid,description]。

HR sends me staff data in excel with the staffid and nationality which I upload as it is. 人力资源部会以我上传的员工身份和国籍以excel格式向我发送员工数据。 Only problem is the nationality column contains data such as Canadian instead of (CAN) or Nigerian instead of (NGA). 唯一的问题是,“国籍”列包含诸如加拿大而不是(CAN)或尼日利亚而不是(NGA)之类的数据。

How do i write a script in sql (or php even) that can update all the natid in tbl_demographics into data that is corresponding the correct natid in tbl_nationality? 如何在sql(甚至php)中编写一个脚本,可以将tbl_demographics中的所有natid更新为与tbl_nationality中的正确natid相对应的数据? I'm using mysql server (and php if necessary). 我正在使用mysql服务器(和php,如果需要的话)。

The data they send: 他们发送的数据:

staffid | natid

201001  | Canadian

202332  | Nigerian

Expected tables look: 预期表外观:

staffid | natid (tbl_demographics)

201001  | CAN
202332  | NGA


natid   | description (tbl_nationality)

CAN     | Canada
NGA     | Nigeria

Create a map where key is full name of country and value is abreviation. 创建一个地图,其中key是国家的全名,值是缩写。 for country field, if key is in map use the value as country else use the key/entry from hr 对于国家/地区字段,如果键在地图中,请将该值用作国家/地区,否则请使用来自hr的键/条目

simply: 只是:

$count = 0;
$result = mysql_query("SELECT * FROM tbl_nationality");
while ($source = mysql_fetch_array($result)) {
  $nationality[$count] = $source['description'];
  $natid[$count] = $source['natid'];
  $count++;
}

$num = 0;
$result = mysql_query("SELECT * FROM tbl_demographics");
while ($source = mysql_fetch_array($result)) {
  $count = 0;
  while (isset($natid[$count])) {
    if ($source['natid'] == $nationality[$count]) {
      $index[$num] = $source['staffid'];
      $correctid[$num] = $natid[$count];
      $num++;
    }
    $count++;
  }
}
$num = 0;
while (isset($index[$num])) {
  mysql_query("UPDATE tbl_demographics SET `natid` = '$correctid[$num]' WHERE `staffid` = '$index[$num]'");
  $num++;
}

which should then select the variables that are incorrect and correct them. 然后应选择不正确的变量并进行更正。

Use this, assuming you place the HR excel data in a table called tbl_excel (only SQL): 假设您将HR excel数据放置在名为tbl_excel的表中(仅SQL),请使用此方法:

UPDATE tbl_demographics d, tbl_nationality n, tbl_excel e
SET d.nat_id = n.natid
WHERE n.description = e.description
  AND e.staffid = d.staffid

This of course needs the descriptions of the data they send (Canada, Canadian) match the descriptions on the tbl_nationality table. 当然,这需要它们发送的数据的描述(加拿大,加拿大)与tbl_nationality表中的描述相匹配。

$query_demo = "SELECT * FROM tbl_demographics";
$res_demo = mysql_query($query_demo);
while ($row = $mysql_fetch_assoc($res_demo)) {
    $query_nat = "SELECT natid FROM tbl_nationality WHERE description='" . $row["description"] . "'";
    $res_nat = mysql_query($query_nat);
    $arr_nat = $mysql_fetch_assoc($res_nat);
    if ($arr_nat) {
        $query_update = "UPDATE tbl_demographics SET natid='" . $arr_nat["natid"] . "' WHERE staffid=" . $row["staffid"];
        mysql_query($query_update);
    }
}

EDIT: This assumes that tbl_demographics initially contains some wrong natids, and that the wrong natids correspond to descriptions found in tbl_nationality. 编辑:这假定tbl_demographics最初包含一些错误的natid,并且错误的natid与tbl_nationality中的描述相对应。

My code loops through the initial 'wrong' tbl_demographics, looks up the corresponding description in tbl_nationality, reads the natid from there, and updates the corresponding entry in tbl_demographics. 我的代码遍历最初的“错误” tbl_demographics,在tbl_nationality中查找相应的描述,从那里读取natid,并更新tbl_demographics中的相应条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM