SQL选择订单按数组中的值索引

发布于 2025-02-07 06:48:58 字数 1111 浏览 4 评论 0原文

我有一个字符串[“ text1”,“ text2”,“ text3”],恰好被保存为json阵列字段tablea

simelidid字符串
1[“ text1”,“ text2”,“ text2”,“ text3”]

和带有带有的tableb一个将包含其中一个字符串的值的列

simolorId字符串其他列
1“ text3”
2“ text1”
3“ text2”
4“ text1”

我想从表中选择,并由String_value订购的结果,但是按照它们出现在数组中的顺序,

    SET @v1 := (SELECT strings FROM tableA where uniqueID = 1);
    SELECT UNIQUEID from TABLEB
    Where 1=1
    Order by  array_index in @v1?

使结果将是所有“ text1”,然后是“ text2”等 顺序)

而是
因为
(不是
中的阵列中的
因为那是它们出现在tablea

Alpahbotical顺序,

I have one array of strings [“text1”, “text2”, “text3”] which happens to be saved as JSON array field on TableA

uniqueIDstrings
1["text1","text2","text3"]

And a TableB with a column that will contain the value of one of those strings

UNIQUEIDSTRINGsome other columns
1“text3”value
2“text1”value
3“text2”value
4“text1”value

I want to select from the table with results ordered by STRING_VALUE but in the order in which they appear in the array

something like

    SET @v1 := (SELECT strings FROM tableA where uniqueID = 1);
    SELECT UNIQUEID from TABLEB
    Where 1=1
    Order by  array_index in @v1?

So that the result would be all the "text1" followed by "text2" etc
(not because of alpahbetical order but because that is the order in which they appear in the array in tableA)

UNIQUEID
2
4
3
1

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑着哭最痛 2025-02-14 06:48:58

您可以:

  • 来加入两个表格。
  • 通过将JSON字段与每个行关联,使用 json_search 函数提取字符串索引,其中“ tableb.string ” iS
  • jes使用替换函数的未签名整数索引,然后是cast
  • 在条款中使用订单中的此索引:
SELECT tableB.UNIQUEID
FROM tableB
INNER JOIN tableA ON 1=1
ORDER BY CAST(REPLACE(REPLACE(
              JSON_SEARCH(tableA.strings, 'one', tableB.STRING),
              '"$[', ''), ']"', ''
         ) AS UNSIGNED),
         tableB.UNIQUEID

检查demo 在这里

You can:

  • join the two tables, by associating the json field to every row,
  • use the JSON_SEARCH function to extract the string index where the "tableB.STRING" is located
  • transform the string index into an unsigned integer index using the REPLACE function followed by CAST
  • use this index inside the ORDER BY clause:
SELECT tableB.UNIQUEID
FROM tableB
INNER JOIN tableA ON 1=1
ORDER BY CAST(REPLACE(REPLACE(
              JSON_SEARCH(tableA.strings, 'one', tableB.STRING),
              '"$[', ''), ']"', ''
         ) AS UNSIGNED),
         tableB.UNIQUEID

Check the demo here.

記柔刀 2025-02-14 06:48:58

使用JSON_TABLE

SELECT b.UNIQUEID
FROM 
    tableB b
    JOIN 
        (
        SELECT * 
        FROM 
            JSON_TABLE
            (
                (SELECT STRS FROM tableA WHERE UNIQUEID = 1), '$[*]' 
                COLUMNS(
                SEQ FOR ordinality, 
                STR varchar(10) path '
表COL定义。

tablea

唯一的 strs

tableb

siless str 其他

(在Mariadb 10.6.5上测试)

) ) rs ) jt ON (b.STR = jt.STR) ORDER BY jt.SEQ ;
表COL定义。

tablea

唯一的strs

tableb

silessstr其他

(在Mariadb 10.6.5上测试)

use JSON_TABLE.

SELECT b.UNIQUEID
FROM 
    tableB b
    JOIN 
        (
        SELECT * 
        FROM 
            JSON_TABLE
            (
                (SELECT STRS FROM tableA WHERE UNIQUEID = 1), '$[*]' 
                COLUMNS(
                SEQ FOR ordinality, 
                STR varchar(10) path '
table col definitionS.

tableA

UNIQUEID STRS

tableB

UNIQUEID STR OTHERS

(tested on mariadb 10.6.5)

) ) rs ) jt ON (b.STR = jt.STR) ORDER BY jt.SEQ ;
table col definitionS.

tableA

UNIQUEIDSTRS

tableB

UNIQUEIDSTROTHERS

(tested on mariadb 10.6.5)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文