asmodin: Werte in Tabelle A gruppieren und in Tabelle B zuordnen

Guten Tag!

Ich benötige wieder etwas Hilfe mit einer MySQL-Sache.

Ausgangssituation:

tabelle_a

ID     Wert (und viele andere Felder)
-----------
1      0
2      0
3      0

tabelle_b

ID     ID_a
-----------
1      1
2      2
3      3
4      1
5      2
6      1
7      1
8      2
9      2

Jetzt steh ich total auf der Leitung.

SELECT COUNT(ID_a) as anzahl, ID_a FROM tabelle_b GROUP BY ID_a;

Dabei kommt Folgendes Raus

anzahl ID_a
-----------
4      1
4      2
1      3

Und das Ergebnis soll jetzt in tabelle_a geschrieben werden. Wie bekomme ich jetzt obriges Ergebnis in mein INSERT-Statement?

Mir ist übrigens klar, dass tabelle_a die Werte mittels JOIN holen könnte, es ist in diesem Fall aber aus Performancegründen wichtig, dass die Summe aus der anderen Tabelle drinnensteht.

Danke im Voraus

  1. yo,

    INSERT INTO tabelle_a (spalte1_die_gefüllt_werden_soll, spalte2_die_gefüllt_werden_soll)
    SELECT COUNT(ID_a) as anzahl, ID_a
    FROM tabelle_b
    GROUP BY ID_a
    ;

    Ilja

    1. INSERT INTO tabelle_a (spalte1_die_gefüllt_werden_soll, spalte2_die_gefüllt_werden_soll)
      SELECT COUNT(ID_a) as anzahl, ID_a
      FROM tabelle_b
      GROUP BY ID_a
      ;

      Hallo, danke für deine Antwort.

      Ich habe mich vorher falsch ausgedrückt. Mit INSERT habe ich's bereits versucht und hatte keinen Erfolg:

      INSERT INTO tabelle_a (id, wert)  
      SELECT id, COUNT(id) as anzahl FROM tabelle_b GROUP BY id  
      ON DUPLICATE KEY UPDATE wert = count;  
      
      

      Das haut aber nicht hin, INSERT werden keine gemacht, UPDATE ebenfalls nicht.

      phpMyAdmin sagt:
      "Eingefügte Zeilen: 0 (die Abfrage dauerte 0.0862 sek.)"

      Wie verpack ich das ordentlich in ein UPDATE-Statement?