1

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

例如我这样做:

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)

这不会起作用,因为子查询中有两个字段..那么如何解决这个问题?

4

2 回答 2

2

您可以使用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)
于 2013-04-03T11:38:07.573 回答
0

试试这个查询

 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
于 2013-04-03T11:39:40.413 回答