Fusion表查询

时间:2015-04-22 15:22:24

标签: sql google-fusion-tables

我想在Google Fusion Table上执行查询。让我们假设我正在使用以下变量组装字符串。如果var sort_factors中只有一个参数,那么一切正常。如何向sort_factors添加第二个参数,以便我可以(例如)按日期降序排序并将响应限制为1000行?

var base         = 'https://www.googleapis.com/fusiontables/v2/query?',
    columns      = 'sql=SELECT+Lat,Lng,Date,Username,TripID',
    from         = '+from+XXXXXXXXXXXXXXXXXXX',
    sort_factors = '+LIMIT+1000+AND+ORDER+BY+Date+DESC',
    key          = '&key=XXXXXXXXXXXXXXXXXXX';

2 个答案:

答案 0 :(得分:1)

LIMIT不是排序,AND错误,LIMIT必须是SQL中的最后一个子句:

'+ORDER+BY+Date+DESC+LIMIT+1000'

条款的顺序是固定的,必须是:

  1. ,其中
  2. group
  3. 订单
  4. limit
  5. 你的代码似乎是Javascript,你最好帮个忙,让JS处理编码。

    可能的方法:

    
    
    var base = 'https://www.googleapis.com/fusiontables/v2/query',
      columns = 'SELECT Lat,Lng,Date,Username,TripID',
      from = 'from fusionTableID',
      //apply a filter when you want to
      where = '',
      //group the results when you want to
      groupby = '',
      orderby = 'ORDER BY DATE DESC',
      limit = 'LIMIT 1000',
      key = 'yourApiKey',
      //do you want a JSONP-response? Add a callback-parameter
      callback = '&callback=functionName',
      //prepare the query;
      sql = encodeURIComponent([columns, from, where, groupby, orderby, limit].join(' ')),
      //prepare the url
      url = [base, '?sql=', sql, callback, '&key=', key].join('');
    
    //see what we got
    document.body.appendChild(document.createTextNode(url));
    
    body {
      font-family: Monospace
    }
    
    
    

    使用所有4个子句进行演示:http://jsfiddle.net/doktormolle/fc47243g/

答案 1 :(得分:0)

I don't know if this completely makes the query work, but you're missing some spaces where you had +:

var base         = 'https://www.googleapis.com/fusiontables/v2/query?',
    columns      = 'sql=SELECT Lat,Lng,Date,Username,TripID ',
    from         = '+from XXXXXXXXXXXXXXXXXXX ',
    sort_factors = '+LIMIT 1000 AND ORDER BY Date DESC ',
    key          = '&key=XXXXXXXXXXXXXXXXXXX';