首页 > 解决方案 > 使用 alasql 时在 SQL 中使用变量时遇到问题

问题描述

这可能是一个初学者的问题,但我不知道如何在 SQL 查询中使用变量。

const CCqry = req.params.CC;  // It contains a string
var values =Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE bornCountryCode = \"%CCqry\" ", [values]);

以上对我不起作用,不知道我做错了什么。

但是下面的一个有效,

const CCqry = req.params.CC;  // It contains a string
var values =Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE bornCountryCode = \"US\" ", [values]);

我指的 JSON 是

{
"laureates": [
{
"id": "1",
"firstname": "Wilhelm Conrad",
"surname": "Röntgen",
"born": "1845-03-27",
"died": "1923-02-10",
"bornCountry": "Prussia (now Germany)",
"bornCountryCode": "DE",
"bornCity": "Lennep (now Remscheid)",
"diedCountry": "Germany",
"diedCountryCode": "DE",
"diedCity": "Munich",
"gender": "male",
"prizes": [
{
"year": "1901",
"category": "physics",
"share": "1",
"motivation": "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
"affiliations": [
{
"name": "Munich University",
"city": "Munich",
"country": "Germany"
}
]
}
]
},
{
"id": "996",
"firstname": "Robert",
"surname": "Wilson",
"born": "1937-05-16",
"died": "0000-00-00",
"bornCountry": "USA",
"bornCountryCode": "US",
"bornCity": "Geneva, NE",
"gender": "male",
"prizes": [
{
"year": "2020",
"category": "economics",
"share": "2",
"motivation": "\"for improvements to auction theory and inventions of new auction formats\"",
"affiliations": [
{
"name": "Stanford University",
"city": "Stanford, CA",
"country": "USA"
}
]
}
]
}
]
}

我在一个外部文件中有这个 JSON 以及许多条目。我找不到任何要弄清楚的文档。

标签: alasql

解决方案


推荐阅读