2016-07-22 42 views
0

下下面的MySQL查詢的查詢返回的結果顯示,具有不同的順序結果「ORDER BY」是相同的兩個查詢:MySQL的 - 儘管在錯誤的順序

表結構

+-----------------+-------------+------+-----+---------+----------------+ 
| Field   | Type  | Null | Key | Default | Extra   | 
+-----------------+-------------+------+-----+---------+----------------+ 
| image_id  | int(10)  | NO | PRI | NULL | auto_increment | 
| property_id  | int(10)  | NO |  | 0  |    | 
| image_title  | text  | NO |  | NULL |    | 
| image_title_id | int(10)  | NO |  | 0  |    | 
| image_index  | smallint(3) | NO |  | 0  |    | 
| image_version | tinyint(1) | NO |  | 2  |    | 
| image_landscape | tinyint(1) | NO |  | 1  |    | 
| image_visible | tinyint(1) | NO |  | 1  |    | 
| image_type  | tinyint(1) | NO |  | 3  |    | 
+-----------------+-------------+------+-----+---------+----------------+ 

TEST 1

查詢:

SELECT image_id, room_text 
FROM property_record_images 
INNER JOIN property_data_rooms ON property_record_images.image_title_id = property_data_rooms.room_id 
WHERE property_id = 1029 
ORDER BY image_index 

結果:

+----------+-----------------+ 
| image_id | room_text  | 
+----------+-----------------+ 
|  2042 | Front elevation | 
|  2043 | Garden to rear | 
|  2044 | Kitchen   | 
|  2045 | Breakfast area | 
|  2046 | Lounge   | 
|  2047 | Master bedroom | 
|  2048 | Studio   | 
+----------+-----------------+ 

TEST 2

查詢:

SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|') 
FROM property_record_images 
INNER JOIN property ON property_record_images.property_id = property.property_id 
WHERE property_record_images.property_id = 1029 
ORDER BY image_index 

結果:

+---------------------------------------------------------------------+ 
| GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|') | 
+---------------------------------------------------------------------+ 
| 2048|2047|2044|2045|2046|2043|2042         | 
+---------------------------------------------------------------------+ 

這是通過隨機記錄中出現的(不同的 「PROPERTY_ID」)所以它不是一個簡單的方法,只是爲第二個查詢反轉ORDER BY。

任何想法爲什麼會發生這種情況,以及我在哪裏查詢出錯?

+0

樣本輸出需要 – e4c5

+0

你有一些示例數據以及我們的一個表結構? – FMashiro

+0

是'image_index'類型的INT? – KaeL

回答

1

看到http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

,我認爲你應該得到一組有序CONCAT:

SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) ORDER BY image_index SEPARATOR '|') 
FROM property_record_images 
INNER JOIN property ON property_record_images.property_id = property.property_id 
WHERE property_record_images.property_id = 1029 
ORDER BY image_index 
+0

哈!我只是意識到這一點,但你擊敗了我!謝謝。 :-) – Reado