将SQL语句转换成多表联查形式的SQL:SELECT t1.*, ( SELECT count( 1 ) FROM t_spjk_jkgcjcxx t2 WHERE TGSH >= '2023-07-13 00:00:00' AND t2.JKDSBBH = t1.JKDSBBH AND t2.tenant_id = 1667005694987583489 ) AS countNum FROM t_spjk_jkdwxx t1 WHERE t1.tenant_id = 1667005694987583489
时间: 2024-04-19 08:26:56 浏览: 135
91 深入探索多表关联的SQL语句到底是如何执行的?(1).pdf
5星 · 资源好评率100%
你可以将这个SQL语句转换成多表联查形式的SQL如下:
SELECT
t1.*,
countNum.count AS countNum
FROM
t_spjk_jkdwxx t1
LEFT JOIN
(SELECT
JKDSBBH,
COUNT(1) AS count
FROM
t_spjk_jkgcjcxx
WHERE
TGSH >= '2023-07-13 00:00:00'
AND tenant_id = 1667005694987583489
GROUP BY
JKDSBBH) AS countNum
ON
countNum.JKDSBBH = t1.JKDSBBH
WHERE
t1.tenant_id = 1667005694987583489
这样就把原来的子查询转换成了一个联查,通过连接条件将两个表关联起来。同时,使用了一个内部查询来计算 countNum 的值,并将其作为一个别名表连接到原始表 t_spjk_jkdwxx 上。
阅读全文