简体   繁体   English

为什么我的查询不返回任何行

[英]Why doesn't my query return any rows

Structure table show here 结构表显示在这里

If I use query: 如果我使用查询:

select idn
from balans as outerB WHERE idn!='' group by idn order by 
ifnull((select sum(innerB.amount) from balans as innerB 
   where innerB.idn = outerB.idn 
   and status='up'), 0) -
ifnull((select sum(innerB.amount) from balans as innerB 
   where innerB.idn = outerB.idn 
   and status='down'), 0) desc
limit 15

I get 2 rows. 我得到2行。

But if I add condition >0 : 但是如果我添加条件>0

select idn from Balans as outerB WHERE idn!='' AND
(
(select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='up') -
(select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='down')
) > 0
group by idn order by 
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='up'), 0) -
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='down'), 0) DESC
limit 15

In result I get 0 rows... 结果我得到0行...

Tell me please where error? 告诉我哪里出错了? Why am I getting 0 rows? 为什么我得到0行?

You didn't include the IFNULL() in the WHERE condition. 您没有在WHERE条件中包括IFNULL() That is the reason you are getting 0 rows instead of 2. The Sums, returned by the subqueries, are NULL (that's what SUM() returns when there is no matching row, despite what one might think that 0 is more logical, it returns NULL .) 这就是为什么要获得0行而不是2行的原因。子查询返回的Sums为NULL (这是当没有匹配的行时SUM()返回的结果,尽管人们可能认为0更合乎逻辑,但它返回NULL 。)

And this added condition, I'd put it in HAVING clause, not WHERE (doesn't make a difference for the results in this case but it may be more efficient): 而这个附加条件,我将其放在HAVING子句中,而不是WHERE (在这种情况下,它不会对结果产生影响,但可能会更有效):

select idn from Balans as outerB WHERE idn !=''
group by idn 
HAVING
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='up'), 0) -
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='down'), 0) > 0
order by 
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='up'), 0) -
ifnull((select sum(innerB.amount) from Balans as innerB 
   where innerB.idn = outerB.idn 
   and type='down'), 0) DESC
limit 15 ;

I don't see a reason to write this query with correlated subqueries. 我看不出用相关子查询编写此查询的原因。 You can use derived tables and simple joins (also corrected the type into status ): 您可以使用派生表和简单联接(还将type更正为status ):

SELECT di.idn
FROM 
    ( SELECT idn
      FROM Balans
      GROUP BY idn
      HAVING idn > ''
    ) AS di
  LEFT JOIN
    ( SELECT idn, SUM(amount) AS up
      FROM Balans
      WHERE status = 'up'
      GROUP BY idn
    ) AS bu
      ON bu.idn = di.idn
  LEFT JOIN
    ( SELECT idn, SUM(amount) AS down
      FROM Balans
      WHERE status = 'down'
      GROUP BY idn
    ) AS bd 
      ON bd.idn = di.idn 
WHERE COALESCE(bu.up, 0) - COALESCE(bd.down, 0) > 0 ;

Test at SQL-Fiddle SQL-Fiddle上测试

It is probably because 可能是因为

 (select sum(innerB.amount) from Balans as innerB 
  where innerB.idn = outerB.idn 
  and type='up')

Isn't a number in all cases, making MySQL get confused. 在所有情况下都不是一个数字,这会使MySQL感到困惑。

As suggested, I would add IFNULL(..) to prevent MySQL thinking your are doing - on a non-number. 如建议的那样,我将添加IFNULL(..)以防止MySQL认为您正在做-在非数字上。 Another option would be to do 0 + ... just before the (select sum...) that should also tell MySQL that we are talking about numbers here. 另一种选择是在(select sum ...)之前执行0 + ... ,这也应该告诉MySQL我们在这里讨论数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM