简体   繁体   English

SQL - 用一个公共字段合并两个表

[英]SQL - Merge two tables with one common field

I've searched and searched but can't quite find what I'm looking for.我已经搜索和搜索,但无法完全找到我要找的东西。 I'm not totally inept when it comes to SQL but this is beyond me.当谈到 SQL 时,我并不是完全无能,但这超出了我的范围。

So I have two table with one common field.所以我有两个表和一个公共字段。

Table - LName表 - LName
Feild Name字段名称

Table - Descripion表 - 描述
Field Name字段名称
Field Add字段添加
Field Job外勤工作

Now, table LName has just the one field and it is populated.现在,表 LName 只有一个字段并已填充。

Table Description has data in all fields except the 'Name' field.表描述在除“名称”字段之外的所有字段中都有数据。

I need to put the data from Field 'Name' in table LName into Field 'Name' in table Description.我需要将表 LName 中字段“名称”中的数据放入表描述中的字段“名称”中。

Either that or merge both tables into one table (File3) that has all the fields but no data.要么将这两个表合并到一个包含所有字段但没有数据的表 (File3) 中。

Appreciate any help.感谢任何帮助。
Barry巴里

Update 2: I was able to craete a logical file that does what I want but only returns 1 record.更新 2:我能够创建一个逻辑文件,它可以执行我想要的操作,但只返回 1 条记录。

The SQL: SQL:

CREATE VIEW MISBXG.TEST_VIEW ( D COLUMN SYSNAM CHAR (8 ) NOT NULL  
 SYSNAM , LT                                                       
 DIADEV ,                                                          
 DITOPG )                                                          
 AS                                                                
 SELECT ALLISERIES.SYSNAM, CMPALTDEV.DIADEV, CMPALTDEV.DITOPG FROM 
ITTOOLS.ALLISERIES ALLISERIES INNER JOIN MISBXG.CMPALTDEV CMPALTDEV
ON ALLISERIES.SYSNAM = CMPALTDEV.SYSNAM WHERE CMPALTDEV.DIADEV <   
ALLISERIES.SYSNAM ;                                                

LABEL ON COLUMN MISBXG.TEST_VIEW                                   
(SYSNAM IS 'System Name' ,                                        
 DIADEV IS 'Alternate           Device' ,                          
 DITOPG IS 'Pager    Name') ; 

Output of Query查询输出

System Name      Alternate       Pager Name
CHEVY                            AS400 PRIM

So now I have to figure a way for this SQL statement to read through all 28 records.所以现在我必须想办法让这个 SQL 语句通读所有 28 条记录。

Barry巴里

Without an index in common this makes this task seemingly impossible... So this example only works by implementing a index of id on each table and setting them in order of the the assigned indexes.没有一个共同的索引,这使得这个任务看起来不可能......所以这个例子只能通过在每个表上实现 id 的索引并按照分配的索引的顺序设置它们来工作。 Sorry if this is confusing...对不起,如果这令人困惑......

UPDATE LNAME DESCRIPTION
INNER JOIN DESCRIPTION ON
   DESCRIPTION.id = LNAME.id 
SET DESCRIPTION.field_name  = LNAME.field_name;

You never really addressed what I was looking for in my comment, and now you've edited the question so my comment doesn't even really make sense.你从来没有真正解决我在评论中寻找的问题,现在你已经编辑了这个问题,所以我的评论甚至没有任何意义。 (For what it's worth, my suggestion of CPYF was probably not a good idea, but my understanding of your situation was, and still is, quite lacking.) (就其价值而言,我对CPYF建议可能不是一个好主意,但我对你的情况的理解过去是,现在仍然是,相当缺乏。)

Based on something you said in your earlier edit, I am wondering if all the records in CMPALTDEV are actually duplicates of each other (with enough copies to match the number of records in ALLISERIES ).根据您在之前的编辑中所说的内容,我想知道CMPALTDEV中的所有记录是否实际上都是彼此重复的(有足够的副本来匹配ALLISERIES的记录ALLISERIES )。 If so, then the simplest thing to do is probably dispense with any attempt to join, and just plug in the values from CMPALTDEV by brute force.如果是这样,那么最简单的方法可能是放弃任何加入的尝试,只需通过蛮力插入CMPALTDEV的值CMPALTDEV For example, if blank and 'AS400 PRIM' are effectively the constant values you are trying to associate with each of the ALLISERIES.SYSNAM values, then make an empty copy of CMPALTDEV (I'll call it CMPALTDEV2 ) and just hard-code the constant values as follows:例如,如果空白和'AS400 PRIM'实际上是您尝试与每个ALLISERIES.SYSNAM值相关联的ALLISERIES.SYSNAM量值,则制作CMPALTDEV的空副本(我将其称为CMPALTDEV2 ),然后将其硬编码常数值如下:

INSERT INTO CMPALTDEV2
  SELECT SYSNAM, '', 'AS400 PRIM' FROM ALLISERIES

On the other hand, if the values in CMPALTDEV vary, and what you really want to do is match by "relative record number", IBM does give you the RRN function to do so:另一方面,如果CMPALTDEV的值不同,并且您真正想要做的是按“相对记录号”进行匹配,IBM 确实为您提供了RRN函数来执行此操作:

INSERT INTO CMPALTDEV2
  SELECT T1.SYSNAM, T2.DIADEV, T2.DITOPG
  FROM ALLISERIES T1                        
    JOIN CMPALTDEV T2 ON RRN(T1) = RRN(T2)

Please note: Using the relative record number is a pretty hackish way to do anything in SQL.请注意:在 SQL 中使用相对记录号是一种非常黑客的方式。 It's full of potential pitfalls, and really goes against what SQL is meant to be.它充满了潜在的陷阱,而且确实与 SQL 的本意背道而驰。 Most implementations of SQL (for other databases) don't have anything analogous to RRN , and best practice is to avoid using it if you can help it, even on the IBM i.大多数 SQL 实现(用于其他数据库)没有任何类似于RRN东西,最佳实践是如果可以帮助它,则避免使用它,即使在 IBM i 上也是如此。

You can create a new table:您可以创建一个新表:

CREATE TABLE new_table AS (SELECT ln.Name, d.Add, d.Job FROM LName ln, Description d);

BUT I don't know why you want to do this... I mean, you need a relation between LName table and Description table, like a common id or something...但是我不知道你为什么要这样做......我的意思是,你需要 LName 表和 Description 表之间的关系,比如一个公共 ID 或其他东西......

What exactly do you want to do?你到底想做什么?

INSERT INTO Description (FieldName) 
SELECT FieldName FROM LName

is what you're looking for.就是你要找的。 This will put all of the FieldNames from the LName table into the Description table - you can then update the other fields.这会将 LName 表中的所有 FieldName 放入 Description 表中 - 然后您可以更新其他字段。

Unless you're trying JOIN the two tables where Decription.FieldName is null and LName.FieldName isn't... which is just not possible.除非您尝试 JOIN 两个表,其中 Decription.FieldName 为 null 而 LName.FieldName 不是...这是不可能的。

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

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