- 此问题在此处已有答案**:
Is storing a delimited list in a database column really that bad?(10个答案)
4小时前关门了。
这是我的疑问
$this->db->query("insert into
booking (plot_id,booked_by_agent,id,totalamount)
values ($a,$agent_id,$userID,$totalamount)
");
我想在plot_id字段中插入4个plot_id,例如1、2、3、4 ......并且booked_by_agent、id、totalamount对于所有4个plot_id都是相同的。我想这样做
plot_id booked_by_agent id totalamount
1,2,3,4 23 12 100000
6条答案
按热度按时间x6h2sr281#
您很可能希望插入4行,例如:
您的查询如下所示:
否则,正如在注解中提到的,您需要将
plot_id
的数据类型更改为varchar
或text
。尽管这样存储id字符串通常不是一个好主意-但如果不了解您的系统和您的需求,这很难说。llycmphe2#
您可以通过插入插入多行,如下所示:
您可以使用foreach循环生成VALUES部分。
fjnneemd3#
首先,将
plot_id
列更改为varchar
或text
数据类型,并将所有值添加到字符串中,如下所示现在插入
$plotid
代替$a
。8yoxcaq74#
首先将
plot_id
字段数据类型更改为varchar
,然后如果plot_id
是类似102
的字符串,则尝试else plot_id是一个接一个的,然后尝试用逗号分隔它,如:-
q8l4jmvw5#
第一件事是需要为plot_id列指定varchar类型,然后就可以这样做了。
假设绘图区ID来自数组。
xtupzzrd6#
1:可以像这样使用while循环
$a = 1; while ($a<=4){ $this->db->query("insert into booking (plot_id,booked_by_agent,id,totalamount) values ($a,$agent_id,$userID,$totalamount) "); $a++; }
2:或者,您可以从数据库中检查具有相同
user id
的agent id
是否已存在于数据库中,而不是仅添加具有新ID的相同。