gpt4 book ai didi

MySQL使用左连接计算查询的行数

转载 作者:行者123 更新时间:2023-11-29 14:11:16 25 4
gpt4 key购买 nike

我有一个如下所示的查询:

 Select x.date, x.id, x.phone,
x.product, xy.policy,xy.date
from (y left join z
on y.customer_id=z.customer_id)
left join x
on x.id=z.id
left join xy
on xy.id=x.id
where x.date > '2000-01-01'
and y.detail =foo
and xy.policy like 'foo_____'
and xy.policy_type = foo;

如何计算返回的行数?

我尝试使用 SQL_CALC_FOUND_ROWS,但无法完全适应此查询。

任何帮助将不胜感激。

谢谢,斯特凡。

最佳答案

最简单的就是添加一个子查询...

 Select x.date, x.id, x.phone,
x.product, xy.policy,xy.date,
(Select Count(*)
From (y left join z on y.customer_id=z.customer_id)
left join x on x.id=z.id
left join xy on xy.id=x.id
where x.date > '2000-01-01'
and y.detail =foo
and xy.policy like 'foo_____'
and xy.policy_type = foo) RecordCount
from (y left join z
on y.customer_id=z.customer_id)
left join x
on x.id=z.id
left join xy
on xy.id=x.id
where x.date > '2000-01-01'
and y.detail =foo
and xy.policy like 'foo_____'
and xy.policy_type = foo;

如果您想要的只是计数,那么:

 Select Count(*) 
From (y left join z on y.customer_id=z.customer_id)
left join x on x.id=z.id
left join xy on xy.id=x.id
where x.date > '2000-01-01'
and y.detail =foo
and xy.policy like 'foo_____'
and xy.policy_type = foo

关于MySQL使用左连接计算查询的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com