【问题标题】:Mysql concat queryMysql 连接查询
【发布时间】:2021-08-16 19:15:45
【问题描述】:

我需要连接一些列来创建查询,像这样

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"
    }
  ]
}

【问题讨论】:

    标签: mysql json concatenation


    【解决方案1】:

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

    id col col2
    1 Hello World
    2 Foo Bar

    跑了

    select concat(col, col2) as x from table

    你会得到结果

    x
    HelloWorld
    FooBar

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

    【讨论】:

    • 明白,谢谢解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2016-05-19
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多