如何从表中的字段获取值

时间:2015-01-09 01:04:40

标签: mysql ruby-on-rails ruby postgresql

我在表格中有一个名为'parameter'的字段,格式为

utf8: "\xE2\x9C\x93"
id: "805265"
plan: initial
acc: "123456"
last: "1234"
doc: "1281468479"
validation: field
commit: Accept

如何从表中查询'acc'值?

数据库:sequelPro

2 个答案:

答案 0 :(得分:1)

> str = 'utf8..... your string here...'
> puts str
utf8: "\xE2\x9C\x93"
id: "805265"
plan: initial
acc: "123456"
last: "1234"
doc: "1281468479"
validation: field
commit: Accept
=> nil
> str.match(/^acc: "(\d+)"/).captures.first
=> "123456"

答案 1 :(得分:0)

如果我理解正确,请PostgreSQL使用split_part

参见此示例

create table star (param text);
insert into star values ('utf8: "\xE2\x9C\x93"'),
                        ('id: "805265"'),
                        ('plan: initial'),
                        ('acc: "123456"'),
                        ('last: "1234"'),
                        ('doc: "1281468479"'),
                        ('validation: field'),
                        ('commit: Accept');

并在 SELECT 查询中使用函数split_part来获取acc:这样的值

select col2 
from (
      select split_part(param, ' ', 1) col1,
             split_part(param, ' ', 2) col2 
      from star
     ) t where col1='acc:'

注意:如果您想按:拆分字段,请使用select split_part(param, ':', 1)split_part(param, ':', 2) col2,以便 WHERE 子句应为where col1='acc' < / p>

sqlfiddle-demo