select opp.ID, opp .PROJECT_NUM , tpmu.id , tpmu.overhaul_id from overhaul_project_plan opp left join t_project_management_user tpmu on tpmu.overhaul_id = opp.ID ;对这个sql返回的数据进行 count
时间: 2024-02-05 09:10:26 浏览: 142
可以使用以下 SQL 语句对返回的数据进行计数:
```
SELECT COUNT(*) FROM (
SELECT opp.ID, opp.PROJECT_NUM, tpmu.id, tpmu.overhaul_id
FROM overhaul_project_plan opp
LEFT JOIN t_project_management_user tpmu ON tpmu.overhaul_id = opp.ID
) AS res;
```
这里使用子查询包裹原始查询结果,然后对子查询结果进行计数。
相关问题
根据id删除 该sql的查询结果 select * from t_project_disclose tpd where tpd.id in ( select tpd.id from t_project_disclose tpd where tpd.project_plan_id not in ( select opp.ID from t_project_disclose tpd join overhaul_project_plan opp on tpd.project_plan_id = opp.ID ) and tpd.version_id = 'd2576fc1e2be4258ac8b624b926cc44c');
根据您提供的 SQL 查询语句,可以使用以下 SQL 语句来根据 id 删除查询结果中的数据:
```
DELETE FROM t_project_disclose WHERE id IN (
SELECT tpd.id FROM t_project_disclose tpd
WHERE tpd.project_plan_id NOT IN (
SELECT opp.ID FROM t_project_disclose tpd
JOIN overhaul_project_plan opp ON tpd.project_plan_id = opp.ID
)
AND tpd.version_id = 'd2576fc1e2be4258ac8b624b926cc44c'
);
```
上述 SQL 语句中,使用了嵌套的 SELECT 语句来获取需要删除的数据的 id,然后使用 DELETE 语句将其从表 `t_project_disclose` 中删除。其中,子查询的条件与原查询语句相同,即 `tpd.project_plan_id NOT IN (SELECT opp.ID FROM t_project_disclose tpd JOIN overhaul_project_plan opp ON tpd.project_plan_id = opp.ID) AND tpd.version_id = 'd2576fc1e2be4258ac8b624b926cc44c'`。请注意备份数据后再进行删除操作。
sql DELETE FROM t_project_disclose WHERE id IN ( SELECT tpd.id FROM t_project_disclose tpd WHERE tpd.project_plan_id NOT IN ( SELECT opp.ID FROM t_project_disclose tpd JOIN overhaul_project_plan opp ON tpd.project_plan_id = opp.ID ) AND tpd.version_id = 'd2576fc1e2be4258ac8b624b926cc44c'); 报以下错误 You can't specify target table 't_project_disclose' for update in FROM clause
这个错误是因为 MySQL 不支持在一个子查询中更新和查询同一个表。
为了解决这个问题,可以使用临时表或者多层嵌套查询来代替子查询。以下是使用多层嵌套查询的示例 SQL 语句:
```
DELETE FROM t_project_disclose WHERE id IN (
SELECT id FROM (
SELECT tpd.id FROM t_project_disclose tpd
WHERE tpd.project_plan_id NOT IN (
SELECT opp.ID FROM t_project_disclose tpd
JOIN overhaul_project_plan opp ON tpd.project_plan_id = opp.ID
)
AND tpd.version_id = 'd2576fc1e2be4258ac8b624b926cc44c'
) AS tmp
);
```
在这个 SQL 语句中,首先使用了一个 SELECT 语句来获取需要删除的数据的 id,然后将其放入一个临时表 tmp 中,最后使用 DELETE 语句将临时表中的数据从表 `t_project_disclose` 中删除。
请注意,如果数据量很大,使用多层嵌套查询可能会导致性能问题,此时可以考虑使用临时表来解决。
阅读全文