所以我有一个数据集,我想根据字符串ORDER BY FIELD(field_name, ...)
在我想要根据另一列对数据集进行分组后对其进行排序。
我尝试使用子查询,但是ORDER BY
在被子查询时似乎忽略了它。
这是我想与GROUP BY setting_id
SELECT *
FROM `setting_values`
WHERE ((`owned_by_type` = 'App\\Models\\Utecca\\User' AND `owned_by_id` = 1 OR ((`owned_by_type` = 'App\\Models\\Utecca\\Agreement' AND `owned_by_id` = 1006))) OR (`owned_by_type` = 'App\\Models\\Utecca\\Employee' AND `owned_by_id` = 1)) AND `setting_values`.`deleted_at` IS NULL
ORDER BY FIELD(owned_by_type, 'App\\Models\\Utecca\\Employee', 'App\\Models\\Utecca\\Agreement', 'App\\Models\\Utecca\\User')
订单工作得很好,但我无法根据我的订单对其进行分组,它总是选择具有最低主键(id)的订单。
这是我的尝试无效。
SELECT * FROM (
SELECT *
FROM `setting_values`
WHERE ((`owned_by_type` = 'App\\Models\\Utecca\\User' AND `owned_by_id` = 1 OR ((`owned_by_type` = 'App\\Models\\Utecca\\Agreement' AND `owned_by_id` = 1006))) OR (`owned_by_type` = 'App\\Models\\Utecca\\Employee' AND `owned_by_id` = 1)) AND `setting_values`.`deleted_at` IS NULL
ORDER BY FIELD(owned_by_type, 'App\\Models\\Utecca\\Employee', 'App\\Models\\Utecca\\Agreement', 'App\\Models\\Utecca\\User')
) AS t
GROUP BY setting_id;
我想用这个示例数据完成的是1行,其中id为3。
查询中所需的结果集应遵守这些规则
setting_id
owned_by_type
与owned_by_id
一起按以下方式agreement = 1006
,user = 1
,employee = 1
进行过滤。setting_id
的1行时,应在owned_by_type
列Employee, Agreement, User
以下是SQLFiddle。
运行MariaDB版本10.2.6-MariaDB
答案 0 :(得分:2)
首先,优化器可以自由地忽略内部ORDER BY。所以,请进一步说明你的意图。
通过它,您可以使用子查询:
Uncaught TypeError: Cannot read property 'params' of undefined
也许你正在做groupwise max ??