2012-04-17 62 views
2

我得到的查詢是「相同的」,除了在一個查詢中,我只從表中檢索一列,而在另一個查詢中,我從同一個表中檢索多個列。否則一切都是一樣的。出於某種原因,這兩個查詢的結果順序不完全相同。怎麼會這樣?所選列可以影響MySQL結果中的行順序嗎?

這是兩個疑問:

SELECT b.bruker_id, b.poeng_j + b.poeng_ss + b.poeng_u + b.poeng_n + b.poeng_s + b.poeng_f + b.poeng_v AS orderColumn 
FROM brukere b 
JOIN soknad s ON b.bruker_id = s.bruker_id 
WHERE s.t_start_y =2013 
AND s.t_start_s =0 
AND YEAR(NOW()) - b.aar >30 
AND YEAR(NOW()) - b.aar <40 
ORDER BY orderColumn DESC 
LIMIT 10 ; 

SELECT b.bruker_id, b.poeng_j + b.poeng_ss + b.poeng_u + b.poeng_n + b.poeng_s + b.poeng_f + b.poeng_v AS orderColumn, b.etternavn, b.fornavn, YEAR(NOW()) - b.aar AS alder, b.poeng_j, b.poeng_ss, b.poeng_u, b.poeng_n, b.poeng_s, b.poeng_f, b.poeng_v, b.kjonn 
FROM brukere b 
JOIN soknad s ON b.bruker_id = s.bruker_id 
WHERE s.t_start_y =2013 
AND s.t_start_s =0 
AND YEAR(NOW()) - b.aar >30 
AND YEAR(NOW()) - b.aar <40 
ORDER BY orderColumn DESC 
LIMIT 10 ; 

第一個查詢返回:

+-----------+-------------+ 
| bruker_id | orderColumn | 
+-----------+-------------+ 
|  92 |   29 | 
|  271 |   28 | 
|  645 |   28 | 
|  323 |   27 | 
|  487 |   27 | 
|  58 |   27 | 
|  76 |   27 | 
|  289 |   26 | 
|  759 |   26 | 
|  128 |   26 | 
+-----------+-------------+ 

而且scond查詢返回:

+-----------+-------------+-----------+----------+-------+------------+------------------+------------------+-------------+-----------+-----------------+------------+-------+ 
| bruker_id | orderColumn | etternavn | fornavn | alder | poeng_j | poeng_ss | poeng_u | poeng_n | poeng_s | poeng_f | poeng_v | kjonn | 
+-----------+-------------+-----------+----------+-------+------------+------------------+------------------+-------------+-----------+-----------------+------------+-------+ 
|  92 |   29 | Tonheim | Kine  | 33 |   4 |    16 |    3 |   0 |   0 |    6 |   0 |  1 | 
|  645 |   28 | Saue  | Laila | 36 |   8 |    16 |    0 |   0 |   0 |    4 |   0 |  1 | 
|  271 |   28 | Grønnevik | Karl  | 38 |   8 |    16 |    0 |   0 |   0 |    4 |   0 |  0 | 
|  76 |   27 | Tornes | Kristina | 39 |   5 |    16 |    0 |   0 |   0 |    6 |   0 |  1 | 
|  487 |   27 | Smestad | Tonje | 34 |   8 |    16 |    0 |   0 |   0 |    0 |   3 |  1 | 
|  58 |   27 | Torjussen | Linn  | 35 |   8 |    16 |    0 |   0 |   0 |    0 |   3 |  1 | 
|  323 |   27 | Gillebo | Tore  | 31 |   5 |    16 |    0 |   0 |   0 |    6 |   0 |  0 | 
|  759 |   26 | Sætren | Grete | 36 |   4 |    16 |    0 |   0 |   0 |    6 |   0 |  1 | 
|  289 |   26 | Gjersøe | Torbjørn | 37 |   4 |    16 |    0 |   0 |   0 |    6 |   0 |  0 | 
|  339 |   26 | Gjøen  | Fredrik | 34 |   4 |    16 |    0 |   0 |   0 |    6 |   0 |  0 | 
+-----------+-------------+-----------+----------+-------+------------+------------------+------------------+-------------+-----------+-----------------+------------+-------+ 

正如你所看到的,順序是不一樣...

回答

2

如果添加一列到每個選擇第一

b.poeng_j + b.poeng_ss + b.poeng_u + b.poeng_n + b.poeng_s + b.poeng_f + b.poeng_v AS orderColumn 

然後更改查詢是ORDER BY orderColumn

然後,您將能夠清楚地看到什麼它是由訂貨。你可能會再看到一些行有orderColumn相同的值,因此排序可能不是不具有第二ORDER BY標準一致,即ORDER BY orderColumn, bruker_id

使用你的第二個查詢我最終會與

SELECT 
    b.bruker_id, 
    b.etternavn, 
    b.fornavn, 
    YEAR(NOW()) - b.aar AS alder, 
    b.poeng_j, b.poeng_ss, 
    b.poeng_u, 
    b.poeng_n, b.poeng_s, 
    b.poeng_f, 
    b.poeng_v, 
    b.kjonn, 
    (b.poeng_j + b.poeng_ss + b.poeng_u + b.poeng_n + b.poeng_s + b.poeng_f + b.poeng_v) AS orderColumn 
FROM brukere b 
JOIN soknad s ON b.bruker_id = s.bruker_id 
WHERE s.t_start_y =2013 
    AND s.t_start_s =0 
    AND YEAR(NOW()) - b.aar >30 
    AND YEAR(NOW()) - b.aar <40 
ORDER BY orderColumn DESC, b.bruker_id ASC 
LIMIT 10 ; 

在您的樣本數據,bruker_id 645和271都將有28

+0

好主意的orderColumn價值!我做了,正如你可以看到orderColumn變量的許多值是相等的。這可能是原因嗎?如果是這樣,爲什麼兩個查詢之間的順序不同? – darwin 2012-04-17 10:46:34

+1

默認情況下,訂單取決於MySQL在內部存儲它們的順序。理論上相同的查詢會返回相同的數據,但它不是100%確定的。如果因此結果順序很重要,那麼最好添加一個或兩個ORDER標準以確保一個ORDER BY子句中的任何重複項具有備份ORDER BY以確保順序一致。 – 2012-04-17 10:51:43

+0

對於您的具體示例,我無法建議精確*爲什麼* MySQl交換了一些行,但是我懷疑它與選定的特定列相關。這可能是第一個查詢基於'soknad'中的行順序,因爲您正在選擇的是來自'brukere'的PK,但第二個依賴於'brukere'中的排序。儘管如此,我無法提供更多的猜測。 – 2012-04-17 10:52:59

相關問題