INSERT INTO TableCSELECT t.word, SUM(COALESCE(a.countA, 0)) AS CountA, SUM(COALESCE(b.countB, 0)) AS countBFROM( SELECT wordA AS word FROM tableA UNION SELECT wordB FROM tableB) AS tLEFT JOIN tableA AS a към t.word =a.wordALEFT JOIN tableB AS b към t.word =b.wordbGROUP BY t.word
Демонстрация на SQL Fiddle
Това ще ви даде:
<предварителен код>| ДУМА | COUNTA | COUNTB ||--------|--------|--------|| ab | 0 | 10 || abc | 25 | 40 || abcd | 29 | 0 || abcde | 45 | 90 || abcdef | 0 | 55 |