MySQL将字段更新为函数的结果

时间:2011-11-24 09:40:43

标签: mysql

mysql> CREATE FUNCTION test ()
    -> RETURNS CHAR(16)
    -> NOT DETERMINISTIC
    -> BEGIN
    -> RETURN 'IWantThisText';
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT test();
+------------------+
|    test()        |
+------------------+
| IWantThisText    | 
+------------------+
1 row in set (0.00 sec)

mysql> UPDATE `table`
    -> SET field = test()
    -> WHERE id = 1
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------------------------------+
| Level   | Code | Message                                                        |
+---------+------+----------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'test' at row 1                      | 
+---------+------+----------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> SELECT field FROM table WHERE id = 1;
+------------------+
| field            |
+------------------+
| NULL             | 
+------------------+
1 row in set (0.00 sec)

我做错了什么?我只想将field设置为test()的返回值 忘记提及field是VARCHR(255)

2 个答案:

答案 0 :(得分:2)

(不是真正的答案,但评论的时间太长)
你能试试这个自成一体的例子吗?

use test;
CREATE TEMPORARY TABLE soFoo (id int auto_increment, field varchar(255), primary key(id));
delimiter $$
CREATE FUNCTION soTest ()
RETURNS CHAR(16)
NOT DETERMINISTIC
BEGIN
    RETURN 'IWantThisText';
END$$
delimiter ;
INSERT INTO soFoo (field) VALUES ('abc'),(NULL);
UPDATE `soFoo` SET field = soTest() WHERE id = 1;
UPDATE `soFoo` SET field = soTest() WHERE id = 2;
SELECT * FROM soFoo;

在我的机器上正常工作:

C:\web\xampp\mysql\bin>mysql -u volker -p
Enter password: ***************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.8 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;
Database changed
mysql> CREATE TEMPORARY TABLE soFoo (id int auto_increment, field varchar(255), primary key(id));
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter $$
mysql> CREATE FUNCTION soTest ()
    -> RETURNS CHAR(16)
    -> NOT DETERMINISTIC
    -> BEGIN
    ->  RETURN 'IWantThisText';
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> INSERT INTO soFoo (field) VALUES ('abc'),(NULL);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> UPDATE `soFoo` SET field = soTest() WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> UPDATE `soFoo` SET field = soTest() WHERE id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM soFoo;

+----+---------------+
| id | field         |
+----+---------------+
|  1 | IWantThisText |
|  2 | IWantThisText |
+----+---------------+
2 rows in set (0.00 sec)

mysql>

您使用的是哪个服务器版本?

答案 1 :(得分:1)

我不确定为什么,但我做了一个r& d并使用以下函数代码获得完美的结果:

mysql> CREATE FUNCTION test ()
    -> RETURNS CHAR(16)
    -> NOT DETERMINISTIC
    -> RETURN 'IWantThisText';

你的其他代码也很好。