select v4.fsmjid from jr_hc_hczl v1 left join jr_hc_hczlentry v2 on v1.fid=v2.fparentid left join jr_hc_smjdjentry v4 on v2.fsmjid=v4.fentryid where v1.fscph=(select MAX(fscph) from jr_hc_hczl where fscph like '%0606%') 优化
时间: 2023-11-22 17:56:50 浏览: 52
这个SQL语句可以进行如下优化:
```sql
SELECT v4.fsmjid
FROM jr_hc_hczl v1
LEFT JOIN jr_hc_hczlentry v2 ON v1.fid=v2.fparentid
LEFT JOIN jr_hc_smjdjentry v4 ON v2.fsmjid=v4.fentryid
WHERE v1.fscph=(SELECT MAX(fscph) FROM jr_hc_hczl WHERE fscph LIKE '%0606%')
```
1. 将表名和字段名写在反引号中,可以防止与SQL关键字冲突。
2. 使用别名简化表名。
3. 避免使用子查询,可以通过将子查询转换为临时表来避免。
优化后的SQL语句如下:
```sql
SELECT v4.`fsmjid`
FROM `jr_hc_hczl` v1
LEFT JOIN `jr_hc_hczlentry` v2 ON v1.`fid`=v2.`fparentid`
LEFT JOIN `jr_hc_smjdjentry` v4 ON v2.`fsmjid`=v4.`fentryid`
JOIN (SELECT MAX(`fscph`) as `max_fscph` FROM `jr_hc_hczl` WHERE `fscph` LIKE '%0606%') t
ON v1.`fscph` = t.`max_fscph`
```
这样可以避免使用子查询,提高查询效率。同时,加上反引号可以避免与SQL关键字冲突。
阅读全文