SELECT 0,sum(number) from ( SELECT inc.disposal_result as results, COUNT() as number FROM soc_data.incidents inc JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id WHERE inc.disposal_result IS NOT NULL and inc.disposal_result ='3' GROUP BY results) a UNION ALL SELECT 1,sum(number) from ( SELECT inc.disposal_result as results, COUNT() as number FROM soc_data.incidents inc JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id WHERE inc.disposal_result IS NOT NULL and inc.disposal_result !='3' GROUP BY results) b 优化sql保证输出的结果为 第一行 0,结果,第二行 1,结果
时间: 2024-02-26 14:58:44 浏览: 36
可以尝试使用如下的 SQL 语句进行优化:
```
SELECT 0, SUM(CASE WHEN inc.disposal_result = '3' THEN 1 ELSE 0 END) AS result_0
FROM soc_data.incidents inc
JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id
WHERE inc.disposal_result IS NOT NULL;
UNION ALL
SELECT 1, SUM(CASE WHEN inc.disposal_result != '3' THEN 1 ELSE 0 END) AS result_1
FROM soc_data.incidents inc
JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id
WHERE inc.disposal_result IS NOT NULL;
```
这里使用了 `CASE WHEN` 语句来计算符合条件的数量,避免了使用子查询的开销,从而提高了查询效率。同时,将 `GROUP BY` 语句去掉,也可以减少查询时间。
相关问题
SELECT t.id,t.order_number,t.registration_Time,t.inspect_unit,t.inspect_form,t.unit_type,t.unit_ids,t.unit_name, t.problem,t.ask_leave_ids,t.ask_leave_name,t.disposal_situation,t.catalog_id,t.street_name,t.check_street_ids,t.check_street_name,t.receive_Unit,sd.dept_name AS receive_Unit_Name FROM tb_duty_inspect_class t LEFT JOIN tb_sys_dept sd ON sd.ID = t.receive_Unit WHERE t.is_delete='0' 我要根据receive_Unit_Name字段中的值来查询unit_name中的值怎么写
您可以使用子查询的方式来实现这个查询,具体的 SQL 语句如下:
```
SELECT t.id,t.order_number,t.registration_Time,t.inspect_unit,t.inspect_form,t.unit_type,t.unit_ids,t.unit_name,
t.problem,t.ask_leave_ids,t.ask_leave_name,t.disposal_situation,t.catalog_id,t.street_name,t.check_street_ids,t.check_street_name,
t.receive_Unit,sd.dept_name AS receive_Unit_Name
FROM tb_duty_inspect_class t
LEFT JOIN tb_sys_dept sd ON sd.ID = t.receive_Unit
WHERE t.is_delete='0' AND t.unit_name IN (
SELECT unit_name
FROM tb_duty_inspect_class
LEFT JOIN tb_sys_dept ON tb_sys_dept.ID = tb_duty_inspect_class.receive_Unit
WHERE tb_sys_dept.dept_name = 'receive_Unit_Name'
)
```
其中,子查询部分会根据 `receive_Unit_Name` 字段的值查询出符合条件的 `unit_name`,外层查询则会根据这些 `unit_name` 的值来筛选出对应的数据记录。
SELECT * FROM wfcs_disposal_account_day_report d ,wfcs_transfer_sheet_disposal_day_summary t, wfcs_transfer_sheet_produce_day_summary p 这样写跟left join 有什么区别
这种写法是使用了 SQL 的隐式连接方式,即使用逗号分隔多个表名,将它们放在 FROM 子句中。这种写法会返回所有表中的行,但并不一定是按照你期望的方式进行关联。
具体来说,这种写法会返回所有表的笛卡尔积,即每个表中的所有行都会与其他表中的所有行进行组合。这意味着,如果你的表中有很多行,那么这种写法将会返回非常大的结果集。而且,由于没有指定任何连接条件,这种写法还可能会导致结果集中包含许多无用的行,这些行并不符合你的需求。
相比之下,LEFT JOIN 语句提供了更加灵活和准确的连接方式,它允许你指定连接条件,只返回符合条件的行,而且还可以使用其他关键字,如 WHERE 子句和 GROUP BY 子句,来进一步筛选和聚合数据。因此,建议在进行多表连接时,优先考虑使用 LEFT JOIN 语句。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)