作者:萌萌美人鱼 | 来源:互联网 | 2023-10-16 14:26
我尝试使用INSERT IGNORE,但是当我再次运行查询时,仍然可以看到。它仍然插入记录。我不想在其中插入重复的记录。
有人可以帮我吗?
INSERT INTO enquiry_location_status
(enquiry_id,location_id,enquiry_status)
SELECT we.id AS enquiry_id,wl.location_id AS location_id,we.status AS enquiry_status
FROM enquiry we
LEFT JOIN wishlist w
ON w.enquiry_id = we.id
LEFT JOIN wishlist_location wl
ON wl.wishlist_id = w.id
WHERE wl.wishlist_id IS NOT NULL;
我的表定义:
CREATE TABLE `enquiry_location_status` (
`id` bigint(20) NOT NULL,`enquiry_id` int(10) NOT NULL,`location_id` int(11) NOT NULL,`enquiry_status` varchar(30) NOT NULL
);
ALTER TABLE `enquiry_location_status`
ADD PRIMARY KEY (`id`);
ALTER TABLE `enquiry_location_status`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
以下是我的数据透视表数据的屏幕截图:
我已经尝试过了,它似乎正在工作,但是速度很慢。 40 K +条记录只需要80秒。
INSERT INTO enquiry_location_status
(enquiry_id,we.status AS enquiry_status
FROM enquiry we
LEFT JOIN wishlist w
ON w.enquiry_id = we.id
LEFT JOIN wishlist_location wl
ON wl.wishlist_id = w.id
WHERE wl.wishlist_id IS NOT NULL
AND NOT EXISTS (SELECT els.enquiry_id,els.location_id,els.enquiry_status
FROM enquiry_location_status els
WHERE els.enquiry_id = we.id
AND els.location_id = wl.location_id
AND els.enquiry_status = we.status)
谢谢