我正在尝试将 MySQL 查询返回的值分配给 R 中的变量,或者 0 以防查询返回空集(未找到记录)。我以为我可以像这样使用 COALESCE:
data<-dbGetQuery(con,"COALESCE(( SELECT id FROM payments WHERE user_id=25578),0)")
但是,我收到一条语法错误消息:
RS-DBI driver: (could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM payments WHERE user_id=25578,0)' at line 1).
我一直在尝试寻找替代方案,但没有成功。感谢帮助!