In recent years, due to large-scale production and widespread use, drugs have posed a potential threat to biological and human health and have received increasing attention. The occurrence and distribution of drugs in urban rivers are influenced by various factors, such as urbanization level, population density, regional geographical and climatic characteristics, and drug consumption habits. In October 2022, the Yongjiang River Basin was divided into four sub-basins: upstream, midstream, tributaries, and downstream. A total of 20 surface water and sediment samples were collected to explore the occurrence, spatial distribution, and ecological risks of drugs in water and sediment. Among the 40 target drugs, 22 drugs were detected in water samples, accounting for 55% of the target drugs, with a maximum concentration of 207.25 ng·L-1, and 12 drugs were detected in sediment, accounting for 30% of the target drug, with a maximum concentration of 153.63 μg·kg-1. The dominant species in water samples were metabolic drugs. In sediment, the dominant species in the upstream and midstream were quinolone antibiotics, while the dominant species in the tributaries and downstream were cardiovascular drugs. Nine drugs were detected in both water and sediment, with an apparent distribution coefficient Kd value of 5.0-385 029 L·kg-1. According to the average lgKoc value, the order from highest to lowest was: sertraline hydrochloride > ofloxacin > clarithromycin > D,L-venlafaxine > roxithromycin > sulfamethoxazole > fluoxetine > metoprolol > carbamazepine. Principal component analysis was used to analyze the source of drugs in water and four principal components explained 76.24% of the variation, corresponding to a source of medical wastewater, a mixed source of domestic wastewater and aquaculture wastewater, an untreated source of rural domestic wastewater, and an agricultural non-point source. Principal component analysis coupled multiple linear regression analysis well predicted the total concentration of drugs in 20 water samples (R2 = 0.868). Three drugs were present in the water sample that posed ecological risks, with risk quotients in the order of 1,7-dimethylxanthine > dehydrated erythromycin > carbamazepine, whereas only carbamazepine in sediment posed ecological risks. The cumulative risk quotient of multiple drugs in the water at 20 sampling sites was 1.22-16.27, all of which belonged to high risk, while the cumulative risk quotient of multiple drugs in sediment was 0.009~0.064, with no risk at site S11 and low risk at all other sites. The research results can be used to analyze the distribution mechanism of drugs between water and sediment, providing a technical basis for the prevention and control of drug pollution and environmental risks in urban rivers.