如何从mysql文本列中提取所需的数据

时间:2013-08-22 20:35:02

标签: mysql sql select

您好我有一个mysql表,其中包含如下数据

mysql> select * from query limit 5\G
*************************** 1. row ***************************
text:   <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427041" STATUS="0" SQLTEXT="/* mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq ) */SELECT @@session.auto_increment_increment"/> 
*************************** 2. row ***************************
text:   <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427032" STATUS="0" SQLTEXT="/* mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq ) */SELECT @@session.auto_increment_increment"/> 
*************************** 3. row ***************************
text:   <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427046" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='DECT'"/> 
*************************** 4. row ***************************
text:   <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427042" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='MKTF'"/> 
*************************** 5. row ***************************
text:   <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427040" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='FLYC'"/> 
5 rows in set (0.00 sec)

现在我想以下面的格式提取结果

+--------------------------+----------------------------------+--------------------------------------+
| ID                       | timestamp                        | text                                 |
+--------------------------+----------------------------------+--------------------------------------+
| CONNECTION_ID="12427042" | TIMESTAMP="2013-08-20T14:07:41"  | "SET sql_mode='STRICT_TRANS_TABLES'" |
+--------------------------+----------------------------------+--------------------------------------+

最终结果根据ID进行排序,并根据所有设置的文本进行分组,然后全部选择然后更新....

ALTERNATIVE:

如果可能,请告诉我如何加载如下文件

<AUDIT>
      <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427046" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='DECT'"/>
      <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427042" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='MKTF'"/>
      <AUDIT_RECORD TIMESTAMP="2013-08-20T14:07:41" NAME="Query" CONNECTION_ID="12427040" STATUS="0" SQLTEXT="select * from MM_CUSTOMER where upper(custcode) ='FLYC'"/>
</AUDIT>

与上述标准一样进入表格。

mysql> desc query;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| ID        | varchar(50) | YES  |     | NULL    |       |
| timestamp | varchar(50) | YES  |     | NULL    |       |
| text      | text        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

理想情况下,您已经解析了这些记录,并且您为每个属性插入了值,而不是在事后解析它们。

但是,如果你坚持使用XML,并且需要从中创建一个真实的表,你可以尝试使用ExtractValue()来获取你想要的部分。

SELECT
    ExtractValue(`text`, '//@TIMESTAMP')     audit_time,
    ExtractValue(`text`, '//@CONNECTION_ID') connection_id,
    ExtractValue(`text`, '//@SQLTEXT')       sql
FROM query;

注意:不要定期执行此操作。理想情况下,您只需执行一次,即可获取这些值并将其放入正确的表中。每次从XML中提取都会破坏MySQL服务器的性能;您不仅最终解析每行的XML,而且没有XML之外的东西可以过滤或排序,基本上每个操作都会强制进行全表扫描。

(哦,非常认真。你如何选择一半的列名作为关键字?TIMESTAMPTEXT是SQL值类型的名称。)