假设我有一个名为“points”的列,我想用点+ X的当前值更新“点”(其中X是任意数字)
例如
UPDATE tablename SET points=points+30 WHERE userID=1
我尝试了以下内容:
我一直在收到错误:
错误2601:Fusion表返回错误。 (我在我的应用程序中查询融合表)响应是:400 Bad Request无效查询:'points'附近的解析错误
有没有办法选择点的当前值,然后UPDATE points = points + x?
编辑: 问题与建议的问题相同,但它并没有解决我的问题。所以我该怎么做?我知道有人来这里编辑我写过的东西,所以用这个帖子做你想做的事情,因为你无论如何都会去。
答案 0 :(得分:1)
FusionTables在其SQL中不支持数学。您需要使用查询来获取当前值,在SQL之外执行必要的数学运算,然后使用新值调用UPDATE。
示例(在Python中):
getCurrent = "SELECT ROWID, points from <tableid> WHERE userID = 1"
resp = FusionTables.query().sqlGet(sql=getCurrent, ...).execute()
rowid = resp['rows'][0]
newPoints = str(int(resp['rows'][1]) + 30)
updatePoints = "UPDATE <tableid> SET points = " + newPoints + " WHERE ROWID = " + rowid
resp = FusionTables.query().sql(sql=updatePoints, ...).execute()
您一次只能更新一行,因此如果您要执行许多操作,可能会更简单/更便宜/更快地暂停更改,直到您有一定数量的操作,然后使用replaceRows
批量更新(即下载所有数据,对各行进行必要的更改,然后重新上传)。如果您的数据集大于10 MB,则有额外的步骤,但这是可行的。
答案 1 :(得分:0)
I had a similar issue. My code may be able to help someone facing this issue using Java.
I needed to update information in rows of a fusion table in many different ways that were unsupported by fusion tables implementation of SQL. I wrote a java program to help me do my updates. (I'm trying to clean up my data in my fusion table.) I also needed to find a nearby large city / state and update fields in rows with this information. Anyway, you can see the entire program on github.
The entire project directory is found on github here: https://github.com/gjkendall/FusionTableModifyJava
The code of interest is in FusionTableSample.java based on Google/Christian Junks example program of how to create, access and modify fusion tables in java. I'm new a java so it might not be the best code.
You will see a lot of commented code where I did different SQL searches for different things I needed to modify. These are there so I can reference them for future searches.
The program first does a SQL search for the rows I want to modify and puts those rows into an array(mylist). The project goes through the array mylist one row at a time, makes it's modifications (currently I needed to access geonames to get city and state info based on locations in my table) and then updates the row with a fusion table SQL call.
The routine that does the updates row by row is "updateRows" shown here:
private static void updateRows(String tableId) throws IOException {
// IOException needed ParseException
count = 1;
mylist.forEach((myRow) -> {
try {
// modify fields in table...
//newAreaName = kt.firstpart(myRow.get(NOTES).toString()); //get Notes first sentence
//newAreaName = newAreaName.replace("'", "''");
//newAreaName += " X01";
//String state = getStateFrmLoc(myRow.get(LOCATION).toString());
//String state = "MX-BCS";
float km;
if ( "AK,MT,NV".contains(myRow.get(STATE).toString()) ) {
km = 180f; // 111.85 miles
} else {
km = 80.5f; // 50 miles
}
BigCity big = new BigCity(myRow.get(LOCATION).toString(), km);
String cityState = big.cityName +", "+big.state;
if (big.population < 10000f) {
System.out.println("Skip for low population :"+myRow.get(NUMBER));
} else {
sqlupdate = "UPDATE " + tableId + " " +
"SET 'City (nearest)' = '" + cityState + "' " +
",'Codes' = '" + myRow.get(CODES).toString() + ",#U1' " +
"WHERE ROWID = " + myRow.get(ROW_ID);
System.out.println("[" + count + "]" + myRow.get(NUMBER) + ": " + sqlupdate);
// do the update...
if (!mtest) { // if testing then don't update
sql_doupdate(sqlupdate);
}
count++;
if ((count % 30) == 0) {
System.out.println("waiting 60 seconds");
TimeUnit.SECONDS.sleep(60); //Fusion Tables allows 30 updates then must wait 1 minute.
}
}
} catch(Exception e){
System.out.println(e.getMessage());
}
});
}
答案 2 :(得分:-3)
如果您使用的是MySQL,则必须使用以下命令禁用安全更新,SET SQL_SAFE_UPDATES = 0;
您可以使用CONCAT()函数,这里是MySQL示例:
SELECT示例:
SELECT point, CONCAT(point, ' ', id)
FROM points_table;
更新示例:
UPDATE points_table
SET point = CONCAT(point, ' ', id);