我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
我不知道它是从哪里来的。堆栈跟踪也不清楚。引发此错误的查询:
Database::executeQuery('CREATE TEMPORARY TABLE tmp_inventory ENGINE=MEMORY '
. 'SELECT id, email_hash, mailing_list_id, ttl, price, last_click, last_view, extra_data '
. 'FROM inventory i INNER JOIN mailing_list ml on i.mailing_list_id = ml.id '
. 'WHERE i.active = 0 AND i.deleted = 1 AND i.completely_deleted = 1 AND i.resting_to < NOW() AND i.next_sync_at < NOW() AND ml.active = 0 '
. 'LIMIT 10;');
所以我有两个表 - inventory 和 mailing_list。库存具有以下结构:
CREATE TABLE `inventory` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email_hash` char(32) NOT NULL,
`mailing_list_id` int(6) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`last_send_at` datetime DEFAULT NULL,
`resting_to` datetime DEFAULT NULL,
`next_sync_at` datetime DEFAULT NULL,
`ttl` datetime DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`completely_deleted` tinyint(1) NOT NULL DEFAULT '0',
`price` int(10) unsigned NOT NULL,
`last_view` datetime DEFAULT NULL,
`last_view_at` datetime DEFAULT NULL,
`last_updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
)
还有邮件列表:
CREATE TABLE `mailing_list` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL,
`price` int(10) unsigned NOT NULL DEFAULT '1000',
`ttl` int(10) unsigned NOT NULL DEFAULT '604800',
`resting_time` int(10) unsigned NOT NULL DEFAULT '0',
`email_from` varchar(255) DEFAULT NULL,
`email_return_path` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
怎么了?
最佳答案
在您的查询中,您将访问 2 个表:FROM 子句中的 inventory
和 INNER JOIN 子句中的 mailing_list
。
这两个表都有一个名为 id
的列,因此您的数据库不知道您引用的是哪一列。
要解决此问题,请在选择中指定表,将 id
替换为 i.id
或 ml.id
关于php - 完整性约束违规错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751782/