Ich suche Hilfe zu folgendem Problem: Ich habe zwei Tabellen Table_1
Spalten sind itemid
, locationid
, quantity
Table_2
Spalten sind itemid
, location1
, location2
, location3
Ich möchte Daten aus Table_1
(nur quantity
Spalte) in Table_2
(in location1
Spalte) kopieren. Die itemid
sind in beiden Tabellen gleich (Table_1
hat doppelte Element-IDs). Aus diesem Grund möchte ich in eine neue Tabelle kopieren und alle Mengen in einer einzigen Zeile mit jeder Position als Spalte beibehalten. Ich verwende die folgende Abfrage, aber es funktioniert nicht
INSERT INTO
Table_2(location1)
(
SELECT qty
FROM Table_1
WHERE locationid = 1 AND Table_1.locationid = Table_2.locationid
)
Wenn table_2
leer ist, versuchen Sie die folgende Einfügeanweisung:
insert into table_2 (itemid,location1)
select itemid,quantity from table_1 where locationid=1
Wenn table_2
bereits die itemid
-Werte enthält, versuchen Sie diese Aktualisierungsanweisung:
update table_2 set location1=
(select quantity from table_1 where locationid=1 and table_1.itemid = table_2.itemid)
INSERT INTO `test`.`product` ( `p1`, `p2`, `p3`)
SELECT sum(p1), sum(p2), sum(p3)
FROM `test`.`product`;