2016-07-22 41 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