我正在尝试将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).
我一直试图寻找替代但不成功的方法。谢谢你的帮助!
答案 0 :(得分:1)
SELECT COALESCE(count(id) > 0,'1','0') as column_name FROM payments WHERE user_id=25578
如果数据可用于特定ID,则将显示“1”,否则将显示“0”。