mysql - Mysql 连接查询
问题描述
我需要连接一些列来创建查询,就像这样
select concat(atributos->'$.listOptions[',valor,'].text') from table
并且最终的查询必须是这样的
select atributos->'$.listOptions[1].text' from table
但它返回一个字符串,我不知道我是否做错了什么。
这是列属性的内容
{
"type": "select",
"description": "Rota",
"default": "",
"required": "0",
"listOptions": [
{
"text": "1 - Jardins",
"value": "1"
},
{
"text": "2 - Praia do Canto/Shop Vix",
"value": "2"
},
{
"text": "3 - Hotéis Vitória/Serra",
"value": "3"
},
{
"text": "6 - Hotéis Vila Velha/Padarias Praia da Costa",
"value": "6"
},
{
"text": "9 - Cariacica",
"value": "9"
},
{
"text": "5 - Vitória/Vila Velha",
"value": "5"
},
{
"text": "10 - Baú/Reboque",
"value": "10"
}
]
}
解决方案
concat
对其参数中的列返回的值进行操作。例如,如果您有这张桌子:
ID | 山口 | col2 |
---|---|---|
1 | 你好 | 世界 |
2 | 富 | 酒吧 |
跑了
select concat(col, col2) as x from table
你会得到结果
X |
---|
你好世界 |
富吧 |
没有办法直接在 MySQL 中动态构建查询的语法而没有很多麻烦。最好的办法是在运行查询的系统层中构建查询,而不是尝试在 SQL 代码本身中进行。例如,如果您将此查询作为 Python 进程的一部分运行,则可以将查询构建为 Python 字符串,然后将其发送到 MySQL 以运行。
推荐阅读
- mongodb - MongoDB 查询 $and 逐步返回匹配所有字段的文档
- python-3.x - PyMesh 中是否有计算体积的函数?
- java - java - 为什么无法将一个类的getter方法识别为java中的另一个类?
- docker - 使用 docker 部署 nodejs、mongodb、redis 应用
- javascript - Firebase Firestore 不会持久化数组
- java - 重构 - 将相似或不同实体的 arrayList 值放入的代码
- sql - 查询用户发布条目次数的最佳方法
- reactjs - 对 SelectInput 使用 combineValidators 来验证 Guid.Empty
- html - 即使我最小化屏幕,如何在 HTML/CSS 中绝对修复文本
- terminal - 如何独立于终端运行 Electron 应用程序?