These verses seem to bring more questions than answers. Though they both speak of the same location, the number killed differs, as does the identity of the opponent. It seems unlikely that they are about the same battle. 2 Samuel 8:5 speaks of the Syrians of Damascus coming to help the king of Zobah, and 22,000 Syrians falling. So, we have 22,000, and 18,000 and 12,000—three different number of dead, quite likely from three separate battles.
1 Chronicles 18:12., a parallel text to 2 Samuel 8:13, says it was Abishai who led David’s army against the 18,000 who were killed, not Joab (Psalm 60:1). Again, it seems likely these are different battles.
An interesting note, Clarke’s Commentary mentions the Targum explanation of Psalm 60:1 is that the 12,000 dead were those who fell of David & Joab’s men in the battle. Clarke goes on to state that this view seems consistent with the tone of the psalm. Though they were victorious over the Edomites, the loss of 12,000 men was too great a sacrifice, and perhaps proof that God had not been with them in that particular battle (see v 1-3, 10).
What is the solution? I don’t really know. But, there are some viable explanations for the differences in the texts.
A response to 1001 Bible Contradictions.