此查询正确返回3个订单,其中一个订单的ID为12.
select * from `orders` where `is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `partner_id` = '2')
如果我想查询该订单,我会添加id = 12,所以我这样做:
select * from `orders` where `is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `partner_id` = '2') and `orders`.`id` = '12'
现在它返回零结果。我服用疯狂的药吗?我没有看到我的语法有任何问题,所有列都明确引用了正确的表等。
如果它有帮助,这将由Laravel自动生成。这是laravel代码:
这会返回3个结果:
$order = Order::completed()
->whereHas('orderPayments', function ($query) use ($partner) {
$query->where('partner_id', $partner->id);
})->get();
这将返回none:
$order = Order::completed()
->whereHas('orderPayments', function ($query) use ($partner) {
$query->where('partner_id', $partner->id);
})->where('id', 12)->first();
编辑:
为了澄清,此查询正确返回订单。
select * from `orders` where `is_completed` = '1' and `orders`.`id` = '12'
执行where id = 12
会返回正确的结果,与exists
一样,但不能一起返回。如果两者都单独完成,则两者都返回正确的结果。这是没有意义的。
编辑:订单表描述:
+------------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| is_completed | tinyint(1) | NO | | 0 | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------------------+------------------+------+-----+---------+----------------+
order_payments表:
+----------------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| order_id | int(11) | NO | MUL | NULL | |
| partner_id | int(11) | NO | MUL | NULL | |
| amount_in_pennies | int(11) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+----------------------------+------------------+------+-----+---------+----------------+
以下是第一个查询的查询结果:
+----+---------+--------------+---------------------+---------------------+
| id | user_id | is_completed | created_at | updated_at |
+----+---------+--------------+---------------------+---------------------+
| 6 | 1 | 1 | 2017-05-20 18:04:08 | 2017-05-24 17:35:37 |
| 11 | 1 | 1 | 2017-05-24 22:54:56 | 2017-05-24 22:55:28 |
| 12 | 1 | 1 | 2017-05-24 23:12:09 | 2017-05-24 23:12:38 |
+----+---------+--------------+---------------------+---------------------+
来自order_payments表的结果:
+----+----------+------------+
| id | order_id | partner_id |
+----+----------+------------+
| 7 | 6 | 2 |
| 12 | 11 | 2 |
| 14 | 12 | 2 |
+----+----------+------------+
而且,为了使事情变得更加灵巧,这个查询确实有效并成功返回结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '2') and `id` = '6';
因此订单ID 6有效,11没有,12没有。
以下是order_payments表的全部内容:
+----+----------+------------+
| id | order_id | partner_id |
+----+----------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 5 | 1 |
| 6 | 6 | 1 |
| 7 | 6 | 2 |
| 8 | 7 | 1 |
| 9 | 8 | 1 |
| 10 | 9 | 1 |
| 11 | 10 | 1 |
| 12 | 11 | 2 |
| 13 | 11 | 1 |
| 14 | 12 | 2 |
| 15 | 12 | 1 |
+----+----------+------------+
我唯一可以想到的可能是这是一个mysql错误,并且它以某种方式匹配订单ID为6的order_payments id,因为它是order_payments id和订单id匹配的唯一出现。这是一个mysql错误还是我需要别名?
而且,为了使事情更加超级辣,这里有一些更时髦的查询结果:
1结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '2') and `id` = '6';
0结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '2') and `id` = '11';
0结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '2') and `id` = '12';
现在,相同的查询但partner_id从2更改为1:
1结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '1') and `id` = '6';
1结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '1') and `id` = '11';
1结果:
select * from `orders` where `orders`.`is_completed` = '1' and exists (select * from `order_payments` where `orders`.`id` = `order_payments`.`order_id` and `order_payments`.`partner_id` = '1') and `id` = '12';
这些应该返回有效的结果,因为对于所有这三个订单,两个合作伙伴都存在付款。时髦的东西!
答案 0 :(得分:2)
在解决问题之后,通过重新编写子查询替换星号(*)来获得查询的正确结果的mysql解决方案实现了#id;'在选择子查询中。
select *
from `orders`
where `is_completed` = '1'
and exists (
select `id`
from `order_payments`
where `orders`.`id` = `order_payments`.`order_id`
and `partner_id` = '2'
)
and `orders`.`id` = '12'
即使这解决了问题,我仍然认为它是mysql中的一个错误,它阻止了原始查询提供正确的结果。