mysql order by agregate函数工作不正确

sauutmhj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(392)

我对ORDERBY中的聚合函数有问题。问题是输出的排序不正确。甚至可以按顺序聚合函数?他们每次都为我工作。。。
(问题出在order by 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”。
(原始查询有更多的选择字段)。
感谢您的回答:)

soat7uwm

soat7uwm1#

我不知道为什么不起作用,我看不出有什么问题。如果有必要的话,你可以一直这样做。

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

相关问题