【问题标题】:Mysql DISTINCT and ORDER BY not workingMysql DISTINCT 和 ORDER BY 不起作用
【发布时间】:2018-06-19 14:31:35
【问题描述】:

我有这个问题:

    SELECT
  DISTINCT (entries.guid),
  locales.*,
  locales.guid as locale_guid,
  entries.*,
  node.category_guid,
  node.title as node_title,
  nodelocs.caption as node_caption,
  nodelocs.uri as node_uri,
  nodelocs.url as node_url
FROM entries AS entries
  JOIN entry_locales AS locales ON (locales.entry_guid=entries.guid)
  LEFT JOIN nodes AS node ON (node.guid=entries.node_guid)
  LEFT JOIN node_locales AS nodelocs ON (nodelocs.node_guid=entries.node_guid AND nodelocs.lang_guid=locales.lang_guid)

  LEFT JOIN metrics as metrics_default ON (entries.guid = metrics_default.entry_guid)
WHERE

  metrics_default.customer_guid='453b894967968204'

  AND metrics_default.metrics='entryview'

  AND locales.lang_guid='4b5747548a5bd457'

  AND locales.state='1' AND entries.node_guid IN  (SELECT child_guid FROM node_children WHERE guid IN ('4b084f280caffbef') )  ORDER BY metrics_default._updated DESC LIMIT 0,10

所以我想获取具有唯一 guid 的项目,并希望通过 'metricts_default' 表中的 '_updated' 字段对其进行排序。但什么也没有发生。 如果我删除 DISTINCT() - 结果很好,但有重复。

【问题讨论】:

  • distinct 不是列上的函数。它适用于所有被选中的列。

标签: mysql select sql-order-by distinct


【解决方案1】:

你误解了独特的 这里的关键字distinct并不意味着你会得到不同的entry.guid,而是不同的行

这意味着具有相同guid但在其他字段中不同的行不会被MySQL淘汰。

【讨论】:

  • 哦...你能告诉我,我怎样才能只获取按 entry.guid 分组并按 'metrics_default._updated' 排序的行?
  • 我认为在从 MySQL 获取行后,您可以使用 Python 等编程语言轻松完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多