把下面sql改写为oracle语法,delete from mdm_enterprise_business_info a inner join temp_asset_table b on a.ASSET_ID=b.id and a.ORG_CODE=b.ASSET_OWNER_CODE and b.BATCH_ID = a.batch_id;
时间: 2024-03-06 15:46:49 浏览: 87
在Oracle中,可以使用以下语法来执行相同的操作:
```
DELETE FROM mdm_enterprise_business_info a
WHERE EXISTS (
SELECT 1
FROM temp_asset_table b
WHERE a.ASSET_ID = b.id
AND a.ORG_CODE = b.ASSET_OWNER_CODE
AND b.BATCH_ID = a.batch_id
);
```
其中,使用了 `EXISTS` 子查询来判断 `a` 表中的记录是否需要被删除。注意,在 Oracle 中, `INNER JOIN` 语法不支持在 `DELETE` 语句中使用。
相关问题
SELECT t.*, m1.MD_DESCRIPTION AS mapping_column_1, m2.MD_DESCRIPTION AS mapping_column_2 FROM supplier_info t INNER JOIN uf_MDM_jyfzsj m1 ON t.VENDOR_CODE = m1.MD_NUM INNER JOIN uf_MDM_jyfzsj m2 ON t.HIS_BUYER_VENDOR_CODE = m2.MD_NUM; supplier_info t t.HIS_BUYER_VENDOR_CODE 不去除其中为null的数据sql
抱歉,我刚才的回答有误。在 SQL 中,使用 `INNER JOIN` 连接两个表时,只有在连接字段的值相等的情况下,才会在结果集中返回这两个表中的数据。因此,在上述 SQL 语句中,如果 `t.HIS_BUYER_VENDOR_CODE` 的值为 `null`,那么它就无法与 `uf_MDM_jyfzsj` 表中的任何一行进行匹配,从而不会出现在结果集中。
如果你想要排除 `t.HIS_BUYER_VENDOR_CODE` 字段为 `null` 的数据,可以将 `INNER JOIN` 改为 `LEFT JOIN`,并在 `WHERE` 子句中添加条件 `t.HIS_BUYER_VENDOR_CODE IS NOT NULL`,具体如下所示:
```sql
SELECT t.*, m1.MD_DESCRIPTION AS mapping_column_1, m2.MD_DESCRIPTION AS mapping_column_2
FROM supplier_info t
LEFT JOIN uf_MDM_jyfzsj m1 ON t.VENDOR_CODE = m1.MD_NUM
LEFT JOIN uf_MDM_jyfzsj m2 ON t.HIS_BUYER_VENDOR_CODE = m2.MD_NUM
WHERE t.HIS_BUYER_VENDOR_CODE IS NOT NULL;
```
在上述 SQL 语句中,使用了两个 `LEFT JOIN` 连接表,这样即使 `t.HIS_BUYER_VENDOR_CODE` 的值为 `null`,它仍然会出现在结果集中。然后在 `WHERE` 子句中添加条件 `t.HIS_BUYER_VENDOR_CODE IS NOT NULL`,就可以排除其中为 `null` 的数据了。
select employee_number, card_number FROM ( SELECT id, employee_number, card_number FROM ( SELECT case when LENGTH(pin) = 5 THEN CONCAT('000', pin) ELSE pin end as employee_number, cardno as card_number, '' AS id from gate_staff union ALL SELECT employee_number, card_number, id FROM gate_mdm_dm_cardinfo WHERE id in ( SELECT max(id) as maxId FROM gate_mdm_dm_cardinfo a INNER JOIN ( SELECT employee_number, MAX(last_update_time) max_date FROM gate_mdm_dm_cardinfo WHERE last_update_time <![CDATA[ >= ]]> #{startTime} AND last_update_time <![CDATA[ <= ]]> #{endTime} GROUP BY employee_number ) b ON a.employee_number = b.employee_number AND a.last_update_time = b.max_date GROUP BY a.employee_number) and status = 1 and public_flag = 'Y' ) as mid group by employee_number, card_number having count(*) = 1 ) AS ccc WHERE ccc.id != ''; 优化这段代码
可以尝试对这段代码进行优化的几个方面:
1. 使用JOIN替代子查询:将gate_mdm_dm_cardinfo表中的子查询替换为JOIN操作,可以提高查询性能。将子查询中的条件移至ON子句中,以减少结果集大小。
```sql
SELECT employee_number, card_number
FROM (
SELECT id, employee_number, card_number
FROM (
SELECT CASE
WHEN LENGTH(pin) = 5 THEN CONCAT('000', pin)
ELSE pin
END AS employee_number,
cardno AS card_number,
'' AS id
FROM gate_staff
UNION ALL
SELECT employee_number, card_number, id
FROM gate_mdm_dm_cardinfo a
INNER JOIN (
SELECT employee_number, MAX(last_update_time) max_date
FROM gate_mdm_dm_cardinfo
WHERE last_update_time >= #{startTime} AND last_update_time <= #{endTime}
GROUP BY employee_number
) b ON a.employee_number = b.employee_number AND a.last_update_time = b.max_date
WHERE a.status = 1 AND a.public_flag = 'Y'
) AS mid
GROUP BY employee_number, card_number
HAVING COUNT(*) = 1
) AS ccc
WHERE ccc.id != '';
```
2. 添加索引:根据查询中涉及的字段,在gate_staff表和gate_mdm_dm_cardinfo表上添加适当的索引,以加快查询速度。
3. 优化查询计划:使用数据库的查询优化工具,如EXPLAIN语句,来分析查询计划并进行优化。根据分析结果,可以考虑调整查询的顺序、添加或删除索引等操作。
请注意,实际优化效果可能因数据库的特性和数据量而有所不同。建议在进行任何更改之前先备份数据库,并在测试环境中进行验证。
阅读全文