无法在MARIA DB的delete语句中使用ALIAS

时间:2019-11-06 10:51:05

标签: mysql mariadb mariasql

我正在尝试从表中删除重复项,但这不允许我使用别名。 尝试了多个论坛中提供的各种解决方案。

查询是

(defn left-fill-zip [l]
  (loop [loc (z/seq-zip l)]
    (if (z/end? loc)
      (z/root loc)
      (recur
        (z/next
          (cond
            (some? (z/node loc)) loc
            (z/left loc) (z/replace loc (z/node (z/left loc)))
            :else loc))))))

(left-fill-zip '("z" nil nil ("a" "b" ("c" nil) "d" nil)))
=> ("z" "z" "z" ("a" "b" ("c" "c") "d" "d"))

4 个答案:

答案 0 :(得分:1)

您可以尝试使用内部联接代替现有的子查询

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

答案 1 :(得分:1)

您可以使用多表DELETE语句:

+------+--------+
|  ID  | Gender |
+------+--------+
| 1001 | MENS   |
| 1002 | MENS   |
| 1001 | WOMENS |
| 1002 | WOMENS |
| 1001 | KIDS   |
| 1002 | KIDS   |
+------+--------+

答案 2 :(得分:1)

您可以尝试执行此操作,因为到目前为止,使用内部内部删除可能真的很危险,因此可以更复杂但更安全,这样您就可以先检查要删除的内容:

 $postData = array(
  'some_variables'         => "some_values",
);

 $postDataJSON= json_encode($postData);

 $postDataCURL= curl_init('www.example.com/post/request');                                                 

 curl_setopt($postDataCURL, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
 curl_setopt($postDataCURL, CURLOPT_POSTFIELDS, $postDataJSON);                                                                  
 curl_setopt($postDataCURL, CURLOPT_RETURNTRANSFER, true);                                                                      
 curl_setopt($postDataCURL, CURLOPT_HTTPHEADER, array(                                                                          
      'Content-Type: application/json;charset=UTF-8',                                                                                
      'Content-Length: ' . strlen($postDataJSON),
      'x-api-key: ' . $api_key,
    )
  );                                                                                                            

 $postDataResult = curl_exec($postDataCURL);
 curl_close($postDataCURL);

答案 3 :(得分:0)

left joinid时,您可以使用primary key删除同一表中的重复项

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;