Doesn't look like you need a join. Just
SELECT code, sum(pay)
FROM table_b
GROUP BY code
Then again...if you want the null one's to show up too, gonna need to use ifnull
SELECT code, ifnull(sum(pay), 0)
FROM table_b
GROUP BY code
Something like that....