开发者

Can I split up the values from one text field into multiple text fields?

开发者 https://www.devze.com 2023-01-03 00:46 出处:网络
I have a text field that stores a combination of three values, \':\' delimited. (开发者_JS百科looks like A:B:C).

I have a text field that stores a combination of three values, ':' delimited. (开发者_JS百科looks like A:B:C).

I'd like to split those values into three actual columns in the table, but I'm not sure of the best way to go about doing this. I'm using sqlite3, any help super appreciated!


First, alter the schema to add the columns you want. I'm assuming you know how to do this.

Second, run an "update" query that splits the data.

update mytable set column1=..., column2=..., column3=...;

The trick is formulating the "..." to extract the right columns from the "source" column.

You could quite likely do something with ltrim(), rtrim() and CASE (see http://www.sqlite.org/lang_expr.html), but you might have an easier time if you defined a user-defined function, perhaps based on length() and substring().


If you're using php, it goes somehitng like this:

PHP:

$explodedStr = explode(":", $delimitedStr);

SQL:

"INSERT INTO tableName (column1, column2, column3)
 VALUES ('".$explodedStr[0]."','".$explodedStr[1]."', '".$explodedStr[2]."
0

精彩评论

暂无评论...
验证码 换一张
取 消