我想要进行插入选择,但在phpmyadmin中我一直收到错误#1292 - 日期值不正确:''对于列'日期'在第1行 现在,选择对它有效,没有错误。我只在添加INSERT语句时才会收到错误,例如这可以
SELECT
`SalesInvoice_id`,
`Date`,
IFNULL(`valueOriginal`+`VATOriginal`,0) as `Amount`
FROM
(SELECT
`salesinvoice`.`SalesInvoice_id`,
`DatePaid` AS `Date`,
ROUND(SUM((`Unit_Cost`*`Quantity`)),2) as `valueOriginal`,
IF(`salesinvoice`.`VAT`=2,ROUND(SUM(((`Unit_Cost`)*`Quantity`)/100*`salesinvoice_products`.`VAT`),2),0) as `VATOriginal`,
`InvoiceType`
FROM `salesinvoice`
LEFT JOIN `salesinvoice_products` ON `salesinvoice`.`SalesInvoice_id` = `salesinvoice_products`.`SalesInvoice_id`
GROUP BY `salesinvoice`.`SalesInvoice_id`
) a
WHERE `InvoiceType`!='3' && `Date`!=''
但是当我这样做时
INSERT INTO `salesinvoice_payments` (`SalesInvoice_id`,`Date`,`Amount`)
SELECT
`SalesInvoice_id`,
`Date`,
IFNULL(`valueOriginal`+`VATOriginal`,0) as `Amount`
FROM
(SELECT
`salesinvoice`.`SalesInvoice_id`,
`DatePaid` AS `Date`,
ROUND(SUM((`Unit_Cost`*`Quantity`)),2) as `valueOriginal`,
IF(`salesinvoice`.`VAT`=2,ROUND(SUM(((`Unit_Cost`)*`Quantity`)/100*`salesinvoice_products`.`VAT`),2),0) as `VATOriginal`,
`InvoiceType`
FROM `salesinvoice`
LEFT JOIN `salesinvoice_products` ON `salesinvoice`.`SalesInvoice_id` = `salesinvoice_products`.`SalesInvoice_id`
GROUP BY `salesinvoice`.`SalesInvoice_id`
) a
WHERE `InvoiceType`!='3' && `Date`!=''
我收到错误。好的这是我的架构
DROP TABLE IF EXISTS salesinvoice_payments;
DROP TABLE IF EXISTS salesinvoice;
DROP TABLE IF EXISTS salesinvoice_products;
CREATE TABLE salesinvoice_payments
(`sipID` int(11) NOT NULL AUTO_INCREMENT,
`salesinvoice_id` int,
`Date` date,
`Amount` int,
PRIMARY KEY (`sipID`))
;
CREATE TABLE salesinvoice (
`SalesInvoice_id` int(11) NOT NULL AUTO_INCREMENT,
`VAT` tinyint(1) DEFAULT NULL,
`DatePaid` date DEFAULT NULL,
`InvoiceType` tinyint(1) DEFAULT 1,
PRIMARY KEY (`SalesInvoice_id`)
);
CREATE TABLE salesinvoice_products (
product_ID int(11) NOT NULL AUTO_INCREMENT,
SalesInvoice_id int(11) DEFAULT NULL,
Description varchar(255) DEFAULT NULL,
Unit_Cost decimal(19, 2) DEFAULT NULL,
Quantity int(11) DEFAULT NULL,
VAT decimal(10, 1) DEFAULT NULL,
PRIMARY KEY (product_ID)
);
INSERT INTO `salesinvoice`
(`SalesInvoice_id`,`VAT`, `DatePaid`, `InvoiceType`)
VALUES
(1,2,'2017-11-07',1),
(2,2,'2017-11-07',1),
(3,2,'2017-11-07',1),
(4,2,'2017-11-07',1),
(5,2,'2017-11-07',1),
(6,2,'2017-11-07',1)
;
INSERT INTO `salesinvoice_products`
(`SalesInvoice_id`, `Description`, `Unit_Cost`,`Quantity`,`VAT`)
VALUES
(3,'Sausage',1.50,5,21),
(3,'Cabbage',2.50,15,21),
(4,'Herring',2.50,15,21),
(4,'Red',2.50,15,21),
(3,'Dongle',2.50,15,21),
(2,'Thingy',2.50,15,21),
(2,'Spaceship',2.50,15,21),
(1,'Car',2.50,15,21),
(1,'Alien',2.50,15,21)
;
INSERT INTO `salesinvoice_payments` (`SalesInvoice_id`,`Date`,`Amount`)
SELECT
`SalesInvoice_id`,
`Date`,
IFNULL(`valueOriginal`+`VATOriginal`,0) as `Amount`
FROM
(SELECT
`salesinvoice`.`SalesInvoice_id`,
`DatePaid` AS `Date`,
ROUND(SUM((`Unit_Cost`*`Quantity`)),2) as `valueOriginal`,
IF(`salesinvoice`.`VAT`=2,ROUND(SUM(((`Unit_Cost`)*`Quantity`)/100*`salesinvoice_products`.`VAT`),2),0) as `VATOriginal`,
`InvoiceType`
FROM `salesinvoice`
LEFT JOIN `salesinvoice_products` ON `salesinvoice`.`SalesInvoice_id` = `salesinvoice_products`.`SalesInvoice_id`
GROUP BY `salesinvoice`.`SalesInvoice_id`
) a
WHERE `InvoiceType`!='3' && `Date`!=''
好的,第一次发布堆栈溢出时,我将展示我的sqlfiddle。 select插入在sqlfiddle中工作正常,但不在phpmyadmin中。但是在phpmyadmin中我收到错误#1292 - 日期值不正确:''对于列'日期'在第1行。 SQLFIDDLE
由于某种原因,日期没有正确发送。
答案 0 :(得分:2)
SELECT时会收到相同的警告。
mysql> SELECT ... WHERE `InvoiceType`!='3' && `Date`!='';
+-----------------+------------+--------+
| SalesInvoice_id | Date | Amount |
+-----------------+------------+--------+
| 1 | 2017-11-07 | 90.75 |
| 2 | 2017-11-07 | 90.75 |
| 3 | 2017-11-07 | 99.83 |
| 4 | 2017-11-07 | 90.75 |
| 5 | 2017-11-07 | 0.00 |
| 6 | 2017-11-07 | 0.00 |
+-----------------+------------+--------+
6 rows in set, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | 1292 | Incorrect date value: '' for column 'Date' at row 1 |
+---------+------+-----------------------------------------------------+
当我们放弃这种情况时,没有任何警告。
mysql> SELECT ... WHERE `InvoiceType`!='3';
+-----------------+------------+--------+
| SalesInvoice_id | Date | Amount |
+-----------------+------------+--------+
| 1 | 2017-11-07 | 90.75 |
| 2 | 2017-11-07 | 90.75 |
| 3 | 2017-11-07 | 99.83 |
| 4 | 2017-11-07 | 90.75 |
| 5 | 2017-11-07 | 0.00 |
| 6 | 2017-11-07 | 0.00 |
+-----------------+------------+--------+
6 rows in set (0.01 sec)
所以它抱怨将日期列与''
进行比较,而不是将值插入表格。
将MySQL日期与''
进行比较是没有意义的。具有DATE
或DATETIME
类型的列不能为空字符串。无效日期转换为0000-00-00,或者如果您使用严格模式,则它们是错误的。您永远不能获得''
的日期列。
您可以获取NULL
的日期列。这可能是你试图测试的。 NULL
和空字符串''
在标准SQL和MySQL中是不同的。
我猜你有Oracle经验,其中NULL
和空字符串在许多上下文中都被视为相同(Oracle的这种行为不是标准SQL)。
如果您要过滤掉IS NOT NULL
,我建议您使用NULL
。
mysql> SELECT ... WHERE `InvoiceType`!='3' AND `Date` IS NOT NULL;
+-----------------+------------+--------+
| SalesInvoice_id | Date | Amount |
+-----------------+------------+--------+
| 1 | 2017-11-07 | 90.75 |
| 2 | 2017-11-07 | 90.75 |
| 3 | 2017-11-07 | 99.83 |
| 4 | 2017-11-07 | 90.75 |
| 5 | 2017-11-07 | 0.00 |
| 6 | 2017-11-07 | 0.00 |
+-----------------+------------+--------+
6 rows in set (0.01 sec)