【发布时间】:2016-08-12 23:46:32
【问题描述】:
有两个表格“items”和“ratings”。项目可以有多个评分。
目前我正在尝试根据评级检索“项目”的排序集合。所以,我需要根据评分的平均值对集合进行排序。
因此,我实现了这个查询。
SELECT items.id, items.description, avg(ratings.value) as average
FROM `items`
INNER JOIN `ratings` ON `ratings`.`item_id` = `items`.`id`
WHERE `items`.`type` IN ('Book')
GROUP BY ratings.item_id
ORDER BY average desc;
Output(I have not pasted the whole description, since it is too long to fit):
+-----+----------------------------+---------+
| id | description | average |
+-----+----------------------------+---------+
| 241 | Into the intrigue and vio | 3.0000 |
| 242 | Teams can be a driving fo | 2.0000 |
| 243 | NULL | 5.0000 |
| 244 | In Temptations of the Wes | 1.0000 |
| 245 | NULL | 4.0000 |
+-----+----------------------------+---------+
在这里,项目没有相应地排序。
现在,当我从选择中删除描述时,结果是正确的。
因此,假设问题是由于文本字段的长度而发生的,我将描述字段的长度限制为较低的值。在这里,查询也正常工作。 (尝试了多个值后,它在512处找到了断点)
SELECT items.id, left(items.description, 512), avg(ratings.value) as average
FROM `items`
INNER JOIN `ratings` ON `ratings`.`item_id` = `items`.`id`
WHERE `items`.`type` IN ('Book')
GROUP BY ratings.item_id
ORDER BY average desc;
输出(对于较小的修整值):
+-----+----------------------------+---------+
| id | left(items.description,25) | average |
+-----+----------------------------+---------+
| 243 | Into the intrigue and vio | 5.0000 |
| 245 | Teams can be a driving fo | 4.0000 |
| 241 | NULL | 3.0000 |
| 242 | In Temptations of the Wes | 2.0000 |
| 244 | NULL | 1.0000 |
+-----+----------------------------+---------+
那么,这是错误还是预期行为,还是我错过了什么?
【问题讨论】:
-
描述文本字段最初有多长?如果您发布两个查询的不同输出示例,也会有所帮助。
-
@Luke :描述是一个字段类型“文本”,因此其中有很长的段落。另外,我已经更新了输出。