首页 > 解决方案 > 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"
    }
  ]
}

标签: mysqljsonconcatenation

解决方案


concat对其参数中的列返回的值进行操作。例如,如果您有这张桌子:

ID 山口 col2
1 你好 世界
2 酒吧

跑了

select concat(col, col2) as x from table

你会得到结果

X
你好世界
富吧

没有办法直接在 MySQL 中动态构建查询的语法而没有很多麻烦。最好的办法是在运行查询的系统层中构建查询,而不是尝试在 SQL 代码本身中进行。例如,如果您将此查询作为 Python 进程的一部分运行,则可以将查询构建为 Python 字符串,然后将其发送到 MySQL 以运行。


推荐阅读