Patients with osteoarthritis and ankylosing spondylitis have lower cancer-related mortality than the general population. We examined risks of solid cancers at 16 sites in elderly patients with knee or hip osteoarthritis (KHOA) or ankylosing spondylitis.In this population-based retrospective cohort study, we used US Medicare data from 1999 to 2010 to identify cohorts of persons with KHOA or ankylosing spondylitis, and a general population group without either condition, who were followed through 2015. We compared cancer incidence among groups, adjusted for age, sex, race, socioeconomic characteristics, geographic region, smoking and comorbidities.We studied 2 701 782 beneficiaries with KHOA, 13 044 beneficiaries with ankylosing spondylitis, and 10 859 304 beneficiaries in the general population group. Beneficiaries with KHOA had lower risks of cancer of the oropharynx, oesophagus, stomach, colon/rectum, hepatobiliary tract, pancreas, larynx, lung, and ovary than the general population. However, beneficiaries with KHOA had higher risks of melanoma, renal cell cancer, and cancer of the bladder, breast, uterus and prostate. Associations were similar in ankylosing spondylitis, with lower risks of cancer of the oesophagus, stomach, and lung, and higher risks of melanoma, renal cell cancer, and cancer of the renal pelvis/ureter, bladder, breast, and prostate.Lower risks of highly prevalent cancers, including colorectal and lung cancer, may explain lower cancer-related mortality in patients with KHOA or ankylosing spondylitis. Similarities in cancer risks between KHOA and AS implicate a common risk factor, possibly chronic NSAID use.