SQL选择订单按数组中的值索引
我有一个字符串[“ 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
uniqueID | strings |
---|---|
1 | ["text1","text2","text3"] |
And a TableB with a column that will contain the value of one of those strings
UNIQUEID | STRING | some 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以:
json_search
函数提取字符串索引,其中“ tableb.string ” iS替换
函数的未签名整数索引,然后是cast
订单中的此索引:
检查demo 在这里。
You can:
JSON_SEARCH
function to extract the string index where the "tableB.STRING" is locatedREPLACE
function followed byCAST
ORDER BY
clause:Check the demo here.
使用
JSON_TABLE
。表COL定义。
tablea
tableb
(在Mariadb 10.6.5上测试)
use
JSON_TABLE
.table col definitionS.
tableA
tableB
(tested on mariadb 10.6.5)