如何在SQL语句中使用变量名? [英] How to use a variable name in a SQL statement?

查看:763
本文介绍了如何在SQL语句中使用变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R调用mySQL语句,其中我在语句外定义了变量,例如

I'm using R to call a mySQL statement, where I define the variable outside the statement e.g.

foo = 23;
dbGetQuery(con, "select surname from names WHERE age = '.foo.' ;")

但这会返回一个空集,我已经在Google周围搜索并尝试了'.& foo. ".foo." '&& foo." 和许多不同的组合,但它们都不起作用,我认为这应该是mysql问题,而不是我遇到的R特定问题,但不确定.通常,变量具有$值,但R中没有.

But this returns an empty set, I've googled around and tried'.&foo.' ".foo." '".&&foo."' and many different combinations, but none of them work, I think this should be a mysql question rather than an R specific problem I'm having, but not sure. Normally variables have $values but not in R.

推荐答案

这应该有效:

foo = 23;

sqlStatement <- paste("select surname from names WHERE age =",foo,'"',sep="")

dbGetQuery(con, sqlStatement;)

这篇关于如何在SQL语句中使用变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆