[英]Why does adding “where id” to a mysql query return zero results?
此查詢正確返回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();
這沒有返回:
$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 bug,它以某種方式與order_payments id 6匹配,該訂單ID與6的訂單ID匹配,因為這是order_payments id和orders ID匹配的唯一情況。 這是一個mysql bug還是我需要使用不同的別名?
並且,為了使事情更超級傻瓜,以下是一些更時髦的查詢結果:
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';
這些應該返回有效的結果,因為這三個訂單中的所有兩個都向兩個伙伴都付款了。 時髦的東西!
解決問題之后,通過重新編寫子查詢,在子查詢的選擇中將星號(*)替換為“ id”,從而實現了mysql為查詢提供正確結果的解決方案。
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中的錯誤,阻止了原始查詢提供正確的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.