Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql脚本变量和max function-2_Mysql - Fatal编程技术网

mysql脚本变量和max function-2

mysql脚本变量和max function-2,mysql,Mysql,我问了一个关于如何将sql函数(max)结果转换成@变量以及如何使用它的问题。我得到的答案是非常直接和有用的(tanx对响应者说:)。不,我在我的程序中使用了相同的机制,我得到了一个不可接受的结果,我将在这里解释: 请看一下我的mysql控制台,一切都是不言自明的。甚至我的问题:): 现在第一次使用该变量: mysql> insert into command(controller_idx,subcontroller_idx,command_idx,controller_id, subco

我问了一个关于如何将sql函数(max)结果转换成@变量以及如何使用它的问题。我得到的答案是非常直接和有用的(tanx对响应者说:)。不,我在我的程序中使用了相同的机制,我得到了一个不可接受的结果,我将在这里解释: 请看一下我的mysql控制台,一切都是不言自明的。甚至我的问题:):

现在第一次使用该变量:

mysql> insert into command(controller_idx,subcontroller_idx,command_idx,controller_id, subcontroller_id,code,status,type,plan_name,timetable_id,offset,ctime,user_name,result) values
(0,0,@max_command_idx+1,'937','SUB0','SMS',0,'CONFIG','NA','NA',0,NOW(),'admin',0);
Query OK, 1 row affected (0.00 sec)
现在,将该变量第二次用于另一个表:

我的问题: 您能告诉我为什么会出现此错误吗?

多谢各位 徖

有关我的表格的更多信息:

mysql> desc csmslist;
+-------------------+-------------+------+-----+---------+-------+
| Field             | Type        | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| controller_idx    | int(11)     | NO   | PRI | NULL    |       |
| subcontroller_idx | int(11)     | NO   | PRI | NULL    |       |
| command_idx       | int(11)     | NO   | PRI | NULL    |       |
| csmslist_idx      | int(11)     | NO   | PRI | NULL    |       |
| phone             | varchar(24) | YES  |     | NULL    |       |
+-------------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> desc command; 
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| Field             | Type        | Null | Key | Default           | Extra                       |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| controller_idx    | int(11)     | NO   | PRI | NULL              |                             |
| subcontroller_idx | int(11)     | NO   | PRI | NULL              |                             |
| command_idx       | int(11)     | NO   | PRI | NULL              | auto_increment              |
| controller_id     | varchar(24) | YES  |     | NULL              |                             |
| subcontroller_id  | varchar(24) | YES  |     | NULL              |                             |
| code              | varchar(12) | YES  |     | NULL              |                             |
| status            | int(11)     | YES  |     | NULL              |                             |
| type              | varchar(24) | YES  |     | NULL              |                             |
| plan_name         | varchar(24) | YES  |     | NULL              |                             |
| timetable_id      | varchar(24) | YES  |     | NULL              |                             |
| offset            | int(11)     | YES  |     | NULL              |                             |
| ctime             | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_name         | varchar(80) | YES  |     | NULL              |                             |
| result            | int(11)     | YES  |     | NULL              |                             |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
14 rows in set (0.01 sec)

命令表有一个自动增量,当您将列设置为null时(这是您正在做的,不管您认为您正在做什么),它只会对它执行一个自动增量

csmslist表没有自动递增,因此插入null失败

改为这样做:

insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,LAST_INSERT_ID(),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null
在insert to
命令中,只需将列设为NULL,让数据库自动完成它的任务

这可能是因为命令表为空,所以它为max返回NULL

顺便说一句,您可以将该查询放在那里,而不是使用变量:

insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,(select max(command_idx) from command),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null

我在我发布的第二个查询中做了。请注意,如果您先插入到命令中,因为最大值会更大,所以不要在插入到csmslist中执行+1。我使用了LAST_insert_ID(),它工作得很好(在命令行和我的实用程序中)。谢谢
insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,LAST_INSERT_ID(),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null
insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,(select max(command_idx) from command),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null