从 table 索引的值列表中获取第一个值

Get the first value from a list of values a table index

我的输入是两列 table,其中 headers _idchange_numchange_num 是一串 comma-separated 数字,对应于更改 ID。例如:

    _id     change_num
    123     4354, 3243, 7893
    456     920, 1232, 9834, 2323

我想获取 change_num 每一行的第一个值,所以我的输出如下所示:

    _id     change_num
    123     4354
    456     920

如何在第一个逗号处停止并忽略其后的所有内容?此外,如果change_numCN开头,我可以忽略它并只获取数字吗?

    _id     change_num
    123     CN4354, 3243, 7893
    456     920, 1232, 9834, 2323

到return

    _id     change_num
    123     4354
    456     920

这是字符串操作。这样的事情应该有效:

select t.id,
       replace(substr(change_num, 1, instr(change_num, ',') - 1), 'CN', '')
from table t;

如您所知,将 ID 存储在逗号分隔的列表中是个坏主意。如果你对数据结构有任何控制,你应该添加一个连接 table.

试试这个:

SELECT _id,SUBSTRING_INDEX(change_num, ',', 1);

您可以使用oracle 10或更高版本中的REGEXP_SUBSTR函数(或其他数据库中的适当函数)并使用正则表达式查询您想要的数据:

select _id, REGEXP_SUBSTR(change_num, '\d+') from tablet;