首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql脚本变量和max函数-2

mysql脚本变量和max函数-2
EN

Stack Overflow用户
提问于 2011-11-03 09:51:02
回答 1查看 262关注 0票数 0

我问了here一个问题,询问如何将sql函数(Max)结果输入@变量以及如何使用它。我得到的答案非常直截了当和有用(tanx给响应者:Tom Mac)。不,我在我的程序中使用了相同的机制,我得到了一个无法接受的结果,我将在这里解释:请看我的mysql控制台,一切都是不言自明的。甚至我的问题:):

代码语言:javascript
复制
> mysql> select max(command_idx)  into @max_command_idx  from command;
> Query OK, 1 row affected (0.00 sec)

现在第一次使用这个变量:

代码语言:javascript
复制
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>插入> csmslist(controller_idx、subcontroller_idx、command_idx、csmslist_idx、phone) >值(0,0,@max_command_idx+1,0,‘+60127929022’);错误1048 (23000):>列'command_idx‘不能为空

.......................................................................

我的问题是:,你能告诉我为什么会有这个错误吗?

谢谢.......................................................................

关于我的桌子的更多信息:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-03 10:00:02

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

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

取而代之的是这样做:

代码语言:javascript
复制
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 command中,只需使列为空,并让数据库自动完成它的操作。

它可能是命令表是空的,所以它返回最大值的NULL。

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7992889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档