首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计数(*)的子查询

计数(*)的子查询
EN

Stack Overflow用户
提问于 2013-04-03 19:35:55
回答 2查看 110关注 0票数 1

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

例如,我这样做了:

代码语言:javascript
复制
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)

这不起作用,因为子查询中有两个字段。那么如何绕过这一点呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-03 19:38:08

您可以使用JOIN而不是子查询:

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2013-04-03 19:39:40

尝试此查询

代码语言:javascript
复制
 SELECT * 
 FROM 
   table1 a 
 INNER JOIN 
   (SELECT 
      count(*) as total, 
      left(number,7) as prefix  
   FROM 
      table1 
   where 
      outcome like '%Passed%' 
   group by 
      prefix 
   order by 
      total desc limit 250)b
 ON 
   a.outcome = 'Fail' AND 
   left(number,7) = b.prefix
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15786272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档