我如何...根据用户选择的选项更改SQL语句 [英] How do I... Change a SQL statement depending on a user's selected option

查看:131
本文介绍了我如何...根据用户选择的选项更改SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用Carto.js创建地图,我正在尝试根据用户从下拉列表中选择的内容更新SQL查询菜单。例如,如果用户选择更改为水果生产,则图层的SQL查询应更改为:



Hi all,

I am making a map with Carto.js and I am trying to update a SQL query depending on what the user has selected from a dropdown menu. For example, if the user selects "change to fruit production", then the layer's SQL query should change to:

SELECT * FROM indicators WHERE fruit ILIKE 'Yes'





我尝试过:



Carto最近更新了,因此整个.js API格式已经改变。使用他们的文档,这就是我到目前为止添加SQL层的方式:





What I have tried:

Carto has recently updated, so there entire .js API formatting has changed. Using their docs, this is how I have added the SQL layer so far:

//Add SQL layer
		const indicatorsSource = new carto.source.SQL(`
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'Yes'     
		`);		

		const indicatorsStyle = new carto.style.CartoCSS(`
		  #layer {
		    polygon-fill: #50ae31;
    		polygon-opacity: 0.65;
		  }
		`);
		const indicators = new carto.layer.Layer(indicatorsSource, indicatorsStyle);





如你所见,我没有想法如何更改特征变量(示例中的grain)取决于用户的选择。



As you can see, I have no idea how to change the feature variable ("grain" in the example) depending on what the user has been selected.

推荐答案

字符串分隔符是否是此处的问题?要在字符串中嵌入单引号,请在字符串的极值上使用双引号。

Is it the string delimiter that's the issue here? To embed single quotes in a string, use double quote on the extremes of the string.
const indicatorsSource = new carto.source.SQL("
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'Yes'     
		");



但我不知道名为'Yes'的'grain'。谷物是小麦,黑麦,燕麦。哦,现在我想起来...... LIKE和ILIKE应该使用的通配符在哪里?以'w'开头的谷物将是这个




However I don't know of a 'grain' named 'Yes'. Grains are wheat, rye, oat. Oh, and now that I think about it... where's the wildcard that LIKE and ILIKE are supposed to use? Getting grain starting with 'w' would be this

const indicatorsSource = new carto.source.SQL("
		  SELECT *
		    FROM indicators WHERE grain ILIKE 'w%'     
		");



这是你想要的更多吗?



HTH,

Mike


Is that more what you're looking for?

HTH,
Mike


这篇关于我如何...根据用户选择的选项更改SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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