2012-12-04 27 views
1

改變剝離外層之後的問題查詢返回的結果是否一致?

我使用的MySQL 5.1

select s.status, street, m.meterpointid 
from meterpoint m inner join account a 
    on (m.accountid = a.accountid) inner join 
    meterservice s 
    on (m.meterpointid = s.meterpointid) 
where a.citystateid=1 and m.meterpointid=3008 and m.lastupdate is not null 
group by status, street ; 

上述查詢返回

1 210 S HWY 3 3008 

select s.status, street, m.meterpointid 
from meterpoint m inner join account a 
    on (m.accountid = a.accountid) inner join 
    meterservice s 
    on (m.meterpointid = s.meterpointid) 
where a.citystateid=1 and m.lastupdate is not null 
group by status, street ; 

但是從上面的查詢其輸出與之前沒有的查詢完全相同不包含1 210 S HWY 3 3008

任何想法?

感謝 那仁

+0

不客氣。此外,由於您是StackOverflow的新用戶,因此我想告訴您,您可以通過查看答案旁邊的勾號來獲得最佳答案並接受最能幫助您的答案。在這個網站上upvote或接受的答案算作「謝謝」。 –

回答

0

在內部查詢的變化是改變外部查詢計數,所以having count(*) = 1不再是真實的。我發現診斷這些事情的最好方法是剝離外層並查看內部查詢,直到我弄清楚發生了什麼。

+0

謝謝。將tr –

-1

不包括測試m.meterpointid=3008可以改變計數。如果根據count(*) = 1,計數不是1,結果將被丟棄。如果第二個查詢的子查詢返回多個記錄,則該結果將被丟棄。

+0

謝謝。第一個查詢也只返回一行。 –

+0

問題是,在第二個查詢中,最內層select是否爲街道'3008 210 S HWY 3'返回多條記錄。嘗試只運行'select s.status,street,m.meterpointid from meterpoint m inner join account a on(m.accountid = a.accountid) inner join meterervice s on(m.meterpointid = s.meterpointid) where a.citystateid = 1和m.lastupdate不爲空 group by status,street' –

+0

做了那個..並且最內層的select沒有返回行..我已經轉貼了這個問題。 –

0

這並不令人感到意外。您正在使用名爲隱藏列的MySQL(錯誤)功能。也就是說,select子句中的列不在group by子句中,並且不在聚合函數中。

在這種情況下,爲該值選擇一個任意值。

要解決這個問題,無論是做:

group by status, street, m.meterpointid 

select s.status, street, min(m.meterpointid) 

這些將修復查詢,以便結果是確定的。