Question

I am trying to copy data from one table to another., T1, T2 are two tables both set up appropriately both have PKs that are UQ. In particular I want to put value R into the table Combined_Data from GZHTable3. R is non-unique and must be that when T1.Aid = T2.Gid, so I have:

INSERT INTO Combined_Data (R)    
SELECT T2.R    
FROM GZHTable3 as T2   
JOIN Combined_Data as T1 ON T1.Aid = T2.Gid;

Aid and Gid are both UQ and PK for their respective tables. As far as I can see it is objecting because both Aid and Gid are PK.

I am not trying to copy them only the value of T2.R, but it must sync across the two tables on Aid, Gid. Why is it objecting and what is the solution? (Some of the data is replicated between the tables but R is not).

Thanks


To clarify:

TABLE 1 (T1)

Aid     R       Data fields .....


UQid1   0      ........ data .........
UQid2   0      ........ data .........

Table 2 (T2)

Gid     R        Different data fields....


UQid6   R6       ....... data ....
UQid12  R12      ....... data ....
UQid23  R23      ....... data ....
UQid1   R1       ....... data ....

T1, T2 are not ordered the same. For each Aid in T1 I have to find the corresponding value of Gid (eg UQid1) in T2 and copy the (strictly) non-zero value of R in that row in T2 into the corresponding row in T1.

All elements of T2 are non-null and for the present purposes are numerics; and T2 is much larger than T1

Was it helpful?

Solution

Your question is confusing, in the initial post you are trying an insert, but in the edit you said this: "For each Aid in T1 I have to find the corresponding value of Gid (eg UQid1) in T2 and copy the (strictly) non-zero value of R in that row in T2 into the corresponding row in T1."

If the latter is truly the case, you should be able to do something like this:

UPDATE table_1 AS t1 SET t1.value = (SELECT t2.value FROM table_2 AS t2 WHERE t2.UQid1 = t1.id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top