不能在 MARIADB 的删除语句中使用 ALIAS
Can't use ALIAS in delete statement in MARIA DB
我正在尝试从 table 中删除重复项,但它不允许我使用别名。
尝试了以多种形式给出的各种解决方案。
查询是,
DELETE FROM `table` AS t1
WHERE EXISTS (
SELECT 1 FROM `table` AS t2
WHERE t2.`col1` = t1.`col1`
AND t2.`col2` = t1.`col2`
AND t2.id > t1.id )
您可以尝试使用内部联接而不是存在子查询
DELETE t
FROM `billing_squarecustomer` t
INNER JOIN (
SELECT t2.`patient_id`.
FROM `billing_squarecustomer` AS t2
INNER JOIN `billing_squarecustomer` AS t1
WHERE t2.`patient_id` = t1.`patient_id`
AND t2.`merchant_id` = t1.`merchant_id`
AND t2.id > t1.id
) tdel = tdel.patient_id = t.patient_id
可以使用多tableDELETE
语句:
DELETE t1
FROM `table` t1
JOIN `table` t2 ON t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND t2.id > t1.id
当 id
为 primary key
时,您可以使用 left join
删除同一个 table 中的重复项
delete t1
from `table` as t1
left join `table` as t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.id < t2.id
where t2.id is not null;
你可以试试这个,它更复杂但到目前为止更安全,因为使用 inner inside delete 可能真的很危险,这样你可以先检查你要删除的内容:
SET @discriminator = 0;
SET @p1 = null;
SET @p2 = null;
CREATE TEMPORARY TABLE temp_rows(
`id` INT PRIMARY KEY,
`index` INT,
`col1` COL1TYPE,
`col2` COL2TYPE
);
INSERT INTO temp_rows (`index`, `col1`, `col2`, `id`)
SELECT CASE WHEN @p1 != col1 OR @p2 != col2 THEN @discriminator := 0 ELSE @discriminator := @discriminator + 1 END AS 'index',
@p1 := col1 AS 'col1',
@p2 := col2 AS 'col2',
id
FROM `schema`.table
ORDER BY col1, col2, id desc;
DELETE FROM table WHERE EXISTS (
SELECT 1 FROM `temp_rows`
WHERE table.`id` = temp_rows.`id`
);
DROP TEMPORARY TABLE temp_rows;
我正在尝试从 table 中删除重复项,但它不允许我使用别名。 尝试了以多种形式给出的各种解决方案。
查询是,
DELETE FROM `table` AS t1
WHERE EXISTS (
SELECT 1 FROM `table` AS t2
WHERE t2.`col1` = t1.`col1`
AND t2.`col2` = t1.`col2`
AND t2.id > t1.id )
您可以尝试使用内部联接而不是存在子查询
DELETE t
FROM `billing_squarecustomer` t
INNER JOIN (
SELECT t2.`patient_id`.
FROM `billing_squarecustomer` AS t2
INNER JOIN `billing_squarecustomer` AS t1
WHERE t2.`patient_id` = t1.`patient_id`
AND t2.`merchant_id` = t1.`merchant_id`
AND t2.id > t1.id
) tdel = tdel.patient_id = t.patient_id
可以使用多tableDELETE
语句:
DELETE t1
FROM `table` t1
JOIN `table` t2 ON t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND t2.id > t1.id
当 id
为 primary key
left join
删除同一个 table 中的重复项
delete t1
from `table` as t1
left join `table` as t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.id < t2.id
where t2.id is not null;
你可以试试这个,它更复杂但到目前为止更安全,因为使用 inner inside delete 可能真的很危险,这样你可以先检查你要删除的内容:
SET @discriminator = 0;
SET @p1 = null;
SET @p2 = null;
CREATE TEMPORARY TABLE temp_rows(
`id` INT PRIMARY KEY,
`index` INT,
`col1` COL1TYPE,
`col2` COL2TYPE
);
INSERT INTO temp_rows (`index`, `col1`, `col2`, `id`)
SELECT CASE WHEN @p1 != col1 OR @p2 != col2 THEN @discriminator := 0 ELSE @discriminator := @discriminator + 1 END AS 'index',
@p1 := col1 AS 'col1',
@p2 := col2 AS 'col2',
id
FROM `schema`.table
ORDER BY col1, col2, id desc;
DELETE FROM table WHERE EXISTS (
SELECT 1 FROM `temp_rows`
WHERE table.`id` = temp_rows.`id`
);
DROP TEMPORARY TABLE temp_rows;