我正在尝试使用select的结果填充表,但我总是收到以下错误:
重复条目'0'表示密钥'PRIMARY'SQL = INSERT INTO
url(r'^feed/basic/(?P<category>[0-9a-zA-Z]+)/$', BasicFeed(), name='basic_feed'),
(fab_bloquer_jours
,id
,day
)VALUES('','4', '2017-12-04 14:55:49')
我的表fab_bloquer_jours是:
date
我的查询是:
1 id Primary int(11)
2 day int(13)
3 date datetime
我通过下拉列表获得$day=$data['fab_bloquer_jours___day'];
$query = $db->getQuery(true);
$query = "INSERT INTO fab_bloquer_jours (day)
SELECT *
FROM (
SELECT DATE_ADD('2018-01-01',
INTERVAL n4.num*1000+n3.num*100+n2.num*10+n1.num DAY ) AS DATE
FROM (
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n1,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n2,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n3,
(
SELECT 0 AS num
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
) AS n4
) AS a
WHERE DATE >= '2018-01-01' AND DATE < '2019-01-01'
AND WEEKDAY(DATE) = '$day'";
$db->setQuery($query);
$query=$db->execute();
值。
我会在这里感谢一些帮助,因为我无法弄清楚如何使它发挥作用。
答案 0 :(得分:0)
删除ID及其空值,请参阅下文:
INSERT INTO `fab_bloquer_jours` (`day`,`date`) VALUES ('4','2017-12-04 14:55:49');
在第二个查询中,select语句错误。您正在尝试插入一列,但您的select语句表示全选。您为select语句所做的任何操作都必须在括号内并作为一列。 e.g。
INSERT INTO fab_bloquer_jours (day) VALUES (your SELECT COLUMNNAME FROM TABLE)
答案 1 :(得分:0)
尝试将表格中的id
列设置为自动递增。然后不要在insert语句中包含它。
因此,对于您的示例,请在将id字段更新为自动增量后尝试此查询:
INSERT INTO fab_bloquer_jours (day,date) VALUES ('4','2017-12-04 14:55:49');