I’ve got to queries that are “identical” except that in the one query I only retrieve one column from the table whereas in the other one I retrieve several columns from the same table. Otherwise everything is identical. For some reason, the order is not exactly the same in the results from the two queries. How can this be?
These are the two queries:
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 ;
The first query returns:
+-----------+-------------+
| bruker_id | orderColumn |
+-----------+-------------+
| 92 | 29 |
| 271 | 28 |
| 645 | 28 |
| 323 | 27 |
| 487 | 27 |
| 58 | 27 |
| 76 | 27 |
| 289 | 26 |
| 759 | 26 |
| 128 | 26 |
+-----------+-------------+
And the scond query returns:
+-----------+-------------+-----------+----------+-------+------------+------------------+------------------+-------------+-----------+-----------------+------------+-------+
| 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 |
+-----------+-------------+-----------+----------+-------+------------+------------------+------------------+-------------+-----------+-----------------+------------+-------+
As you can see, the order is not the same…
If you add a column to each SELECT first
Then change the queries to be
ORDER BY orderColumnYou will then be able to see clearly what it is ordering by. You will probably then see that some of the rows have the same value in orderColumn, and so the ordering may not be consistent without having a second ORDER BY criteria, i.e.
ORDER BY orderColumn, bruker_idUsing your second query I would end up with
In your sample data, bruker_id 645 and 271 both would have an orderColumn value of 28.