mysql order by agregate函数无法正常工作

时间:2018-07-13 11:51:08

标签: mysql sorting sql-order-by min

我对汇总函数的排序有疑问。问题是输出的排序不正确。甚至可以按顺序汇总功能?他们每次都为我工作...

  

(问题是通过MIN()函数排序的)。

我尝试将所有变量转换为相同的数据类型,但是效果不佳。

查询:

SELECT SQL_CALC_FOUND_ROWS
  COALESCE(MIN(IF(FIND_IN_SET("O", p.campaign), NULL, p.price) * (1 + v.vat / 100) * (1 - p.discount / 100)), p.price) as test,
  p.id as `id`

FROM
  products p
  LEFT JOIN products_variations pv ON p.id = pv.id_product
  LEFT JOIN photos_products_relation ppr ON p.id = ppr.id_product AND ppr.show_in_lead = 'Y'
  LEFT JOIN producers pr ON p.producer = pr.id
  INNER JOIN products_in_sections ps ON p.id = ps.id_product
  INNER JOIN vats v ON p.vat = v.id

WHERE ((pv.figure = 'Y') OR (pv.figure IS NULL)) AND (pr.active = 'Y' OR pr.id IS NULL) AND (
  ((COALESCE(pv.in_store, p.in_store) > 0) AND (ps.id_section IN (1868, 1855, 1758, 1746, 1875, 1947, 1870, 1952, 1895, 1942, 1737, 1741, 1744, 1828, 1887, 1762, 1773, 1893, 1781, 1897, 1929, 1920, 1896, 1908, 1751, 1775, 1782, 1770, 1778)) AND (p.figure = 'Y') AND
   (NOT FIND_IN_SET('O', coalesce(pv.campaign, '')))) AND (1)) AND (p.id_model IS NOT NULL)
GROUP BY p.id_model
ORDER BY COALESCE(MIN(IF(FIND_IN_SET("O", p.campaign), NULL, p.price) * (1 + v.vat / 100) * (1 - p.discount / 100)), p.price) ASC
LIMIT 36 OFFSET 0;

输出:

https://i.stack.imgur.com/qeK9f.png

如您所见,列测试排序不正确...

通常我会做

ORDER BY test

可以,但是我不希望在输出中输入字段“ test”。

(原始查询还有很多选择字段)。

感谢您的回答:)

1 个答案:

答案 0 :(得分:0)

我不知道为什么它不起作用,我看不出有什么问题。如有必要,您总是可以做这样的事情。

SELECT ... (All columns but test)
FROM (  your query here as subquery) a
ORDER BY test ASC