MySQL查询用来填充表中包含该表的其他数据

时间:2013-09-11 14:29:30

标签: mysql sql

我有一个包含一组数据的表,如

record_date  |  id   | category | model    | name | alternate_name
-------------------------------------------------------------------
9/1/2012     |  N42  |   X      |  ABC     | blah |  blah
10/1/2011    |  N42  |   Y      |  No Code | #N/A |  #N/A
6/1/2012     |  N42  |   X      |  DEF     | xxxx |  yyyy
7/1/2011     |  N42  |   Z      |  No Data | #N/A |  #N/A

由于数据集不完整,我想用包含id字段匹配数据的最新记录中的数据填充缺失的数据(model,name,alternate_name)。

即。我希望它看起来像这样

record_date  |  id   | category | model    | name | alternate_name
-------------------------------------------------------------------
9/1/2012     |  N42  |   X      |  ABC     | blah |  blah
10/1/2011    |  N42  |   Y      |  ABC     | blah |  blah
6/1/2012     |  N42  |   X      |  DEF     | xxxx |  yyyy
7/1/2011     |  N42  |   Z      |  ABC     | blah |  blah

3 个答案:

答案 0 :(得分:1)

这是一种使用三个相关子查询的方法:

update "table" t
    set model = (select model from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and model <> 'No Code' order by t2.record_date limit 1),
        name = (select name from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and name <> '#N/A' order by t2.record_date limit 1),
        alternate_name  = (select alternate_name from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and alternate_name <> '#N/A' order by t2.record_date limit 1)      
    where model = 'No Code' and name = '#N/A' and alternate_name = '#N/A';

我建议你在每一行都有一个唯一的ID。

答案 1 :(得分:0)

假设您没有数据表示NULL,您可以使用这些查询来更新一列。您可以使用IS NULL

之类的内容替换= "No Data"
UPDATE tableName A JOIN tableName B ON A.id = B.id
SET A.dummyID = B.dummyID
WHERE A.dummyID IS NULL AND B.dummyID IS NOT NULL;

请参阅example

您应该对其他列运行类似的查询。您可以将这些查询合并为一个,但这会使查询复杂化并使其可读性降低。

假设您有另一个名为anotherColumn的列,"N/A"表示没有数据,您可以使用以下查询 -

UPDATE tableName A JOIN tableName B ON A.id = B.id JOIN tableName C ON A.id = C.id
SET A.dummyID = B.dummyID, C.anotherColumn = B.anotherColumn
WHERE A.dummyID IS NULL AND B.dummyID IS NOT NULL AND
C.anotherColumn = "N/A" AND B.anotherColumn <> "N/A";

请参阅example

答案 2 :(得分:0)

感谢您的建议,但似乎都没有做得很好。我最后分两步完成了这个。

use myDB;
drop table if exists tmp_myTable;
create temporary table tmp_myTable like myTable;
insert into tmp_myTable select * from myTable as t1
  where record_date in (select max(record_Date)
    from myTable where id=t1.id 
    and (model!="No Code" or model="No Data")
    and name!="#N/A")
  and (category="X"
    or category="Y"
    or category="Z")
  and (model!="No Code" or model="No Data")
  and name!="#N/A";

update myTable as t1 join tmp_myTable as t2 on t1.id=t2.id
  set t1.model=t2.model, 
      t1.name=t2.name,
      t1.alternate_name=t2.alternate_name
  where (t1.category="X"
    or t1.category="Y"
    or t1.category="Z")
  and (t1.model="No Code" or t1.model="No Data")
  and t1.name="#N/A";