简体   繁体   English

mysql嵌套查询不适用于AND条件

[英]mysql nested query not working with an AND condition

I don't understand this: the two following queries are almost identical, the only bit that differs is the "and o.shop_id=1" part, yet they don't produce the expected results. 我不明白这一点:以下两个查询几乎相同,唯一不同的是“和o.shop_id = 1”部分,但它们没有产生预期的结果。

The first query: 第一个查询:

select count(*) as count
from ek_order o        
where 1


and  
(
  select
  s.code
  from ek_order_status s
  inner join ek_order_has_order_status h on h.order_status_id=s.id
  where o.id=h.order_id

  order by h.date DESC
  limit 0,1

) in ('preparing_order')

Result of the query: 1 查询结果:1

The second query: 第二个查询:

select count(*) as count
from ek_order o        
where 1

and o.shop_id=1


and  
(
  select
  s.code
  from ek_order_status s
  inner join ek_order_has_order_status h on h.order_status_id=s.id
  where o.id=h.order_id

  order by h.date DESC
  limit 0,1

) in ('preparing_order')

Result of the query: 0 ( I expected 1! ) 查询结果:0( 我预期为1!

My schema structure looks like this: 我的架构结构如下所示:

ek_order: ek_order:

  • id: pk 编号:pk
  • shop_id: nullable fk shop_id:可为空的fk
  • ...other fields not relevant here ...其他与此处无关的字段

ek_order_has_order_status: ek_order_has_order_status:

  • id: pk (I noticed it was not needed, but too late...) id:pk(我注意到这不是必需的,但是为时已晚...)
  • order_id: fk order_id:fk
  • order_status_id: fk order_status_id:fk
  • date: datetime 日期:datetime
  • ...other fields not relevant here ...其他与此处无关的字段

ek_order_status: ek_order_status:

  • id: pk 编号:pk
  • code: str 代码:str
  • ...other fields not relevant here ...其他与此处无关的字段

The data 数据

The ek_order_has_order_status table has a row that looks like this: ek_order_has_order_status表的行如下所示:

  • id: 182 编号:182
  • order_id: 283, order_id:283,
  • order_status_id: 7 (this is the 'preparing_order' code from ek_order_status ) order_status_id:7(这是ek_order_status的“ preparing_order”代码)

and in the ek_order table we can find that shop_id is indeed 1: 在ek_order表中,我们可以发现shop_id的确是1:

  • id: 283 id:283
  • shop_id: 1 shop_id:1

So what I don't understand is why the second query returns 0 instead of 1. I tested all bits of the query apart and they seem to work (I tested the inner query first, then the outer query, both with success), but then, adding the "and o.shop_id=1" part makes the complete query fail. 因此,我不明白的是为什么第二个查询返回0而不是1。我分开测试了查询的所有位,它们似乎都起作用(我先测试了内部查询,然后测试了外部查询,都成功了),但是然后,添加“和o.shop_id = 1”部分会使整个查询失败。

Does anybody know why? 有人知道为什么吗? and/or have a fix. 和/或有修复。 (I could resort to do two queries, but for the sake of improving my sql knowledge I would prefer to understand the bottom of this). (我可以求助于两个查询,但是为了提高我的sql知识,我希望理解其底部)。

Ps: basically, I'm trying to get the number of orders containing the 'preparing_order' status as its final/latest status, knowing that an order can have multiple statuses. 附:基本上,我在知道一个订单可以有多个状态的情况下,尝试获取包含“ preparing_order”状态作为其最终/最新状态的订单数量。

In your second query: 在第二个查询中:

SELECT COUNT(*) as count
FROM ek_order o        
WHERE 1
AND o.shop_id=1
...

you are taking a count of the entire ek_order table, and it therefore does not even make sense to speak of the shop_id value for a single record. 您要对整个ek_order表进行计数,因此,为单个记录讲shop_id值甚至都没有意义。 I suspect that what you intend is if you find any record which has a shop_id value of 1, then this passes as a logical true. 我怀疑您打算做的是,如果找到任何具有shop_id值为1的记录,则该记录作为逻辑true传递。 If so, then you can use EXISTS : 如果是这样,则可以使用EXISTS

SELECT COUNT(*) as count
FROM ek_order o        
WHERE 1
AND EXISTS (SELECT 1 FROM ek_order WHERE shop_id=1)
AND ...

If this doesn't work, then another option would be to take the max value of shop_id : 如果这不起作用,那么另一种选择是采用shop_id的最大值:

SELECT COUNT(*) as count
FROM ek_order o        
WHERE 1
AND MAX(shop_id) = 1
AND ...

This would make sense if you are certain that this table only has a single record and you just want to inline its value alongside the count. 如果您确定该表只有一条记录,并且只想将其值与计数内联,则这将很有意义。

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

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