gpt4 book ai didi

mysql - 带有 count(*) 的子查询

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

如何从子查询count(*)数据中获取关联left(number,7)失败的数据?

例如我这样做:

SELECT * FROM table1 WHERE outcome = 'Fail' AND left(number,7) = 
(SELECT count(*) as total, left(number,7) as prefix
FROM table1 where outcome like '%Passed%'
group by prefix order by total desc limit 250)

这行不通,因为子查询中有两个字段..那么如何解决呢?

最佳答案

您可以使用JOIN 代替子查询:

SELECT t1.*, t2.total, ... 
FROM table1 AS t1
INNER JOIN
(
SELECT count(*) as total, left(number,7) as prefix
FROM table1
where outcome like '%Passed%' AND outcome = 'Fail'
group by prefix
order by total desc limit 250
) AS t2 ON t2.prefix = left(t1.number,7)

关于mysql - 带有 count(*) 的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786272/

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