将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 13:26:56 浏览: 140
SQL语句的多表查询
你可以将这个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 上。
阅读全文