The story
Current phrasing from 1867. Idea present from the 18th century. Earlier expressions include "Well, Miss, you’ll have a sad Husband, you have such good Luck at Cards" (Swift, Polite Conversation, 1738) and "lucky in play, unlucky in love" (1846). A similar French saying "Malheureux en amour, heureux au jeu" is attested from 1850.
Etymology from Wiktionary, CC BY-SA 4.0.