Add sorting to this select.

huangapple go评论108阅读模式
英文:

Add sorting to this select

问题

以下是您要翻译的内容:

**PostgreSQL 15.2**

select
       current.id as currentId,
	   current.date as currentDate,
	   current.position as currentPosition,
	   current.relevant_url as currentRelevantUrl,
	   current.restrictions_probability as currentRestrictionsProbability,
	   current.url_changed as currentUrlChanged,
	   current.identifier_id as currentIdentifierId,
	   current.phrase_id as phraseId,
	   previous.id as previousId,
	   previous.date as previousDate,
	   previous.position as previousPosition,
	   previous.relevant_url as previousRelevantUrl,
	   previous.restrictions_probability as previousRestrictionsProbability,
	   previous.url_changed as previousUrlChanged,
	   previous.identifier_id as previousIdentifierId,
	   semantics_clusters.id as clusterId,
	   semantics_clusters.name as clusterName,
	   site_pages.id as pageId,
	   site_pages.url as pageUrl,
	   site_pages.site_id as siteId,
	   site_sites.url as siteUrl,
	   semantics_core_phrases.frequency as frequency
from (
select
       id,
	   date,
	   position,
	   relevant_url,
	   restrictions_probability,
	   url_changed,
	   identifier_id,
	   phrase_id
from overoptimisation
where identifier_id = 1) current

left join (
select
       id,
	   date,
	   position,
	   relevant_url,
	   restrictions_probability,
	   url_changed,
	   identifier_id,
	   phrase_id
from overoptimisation
where identifier_id = 1) previous

on current.phrase_id = previous.phrase_id
inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
inner join site_pages on semantics_clusters.page_id = site_pages.id
inner join site_sites on site_pages.site_id = site_sites.id

这个查询有效。但我未能添加排序。

我如何为以下字段添加排序?

  1. siteId
  2. pageId
  3. clusterId
  4. frequency
英文:

PostgreSQL 15.2

select
       current.id as currentId,
	   current.date as currentDate,
	   current.position as currentPosition,
	   current.relevant_url as currentRelevantUrl,
	   current.restrictions_probability as currentRestrictionsProbability,
	   current.url_changed as currentUrlChanged,
	   current.identifier_id as currentIdentifierId,
	   current.phrase_id as phraseId,
	   previous.id as previousId,
	   previous.date as previousDate,
	   previous.position as previousPosition,
	   previous.relevant_url as previousRelevantUrl,
	   previous.restrictions_probability as previousRestrictionsProbability,
	   previous.url_changed as previousUrlChanged,
	   previous.identifier_id as previousIdentifierId,
	   semantics_clusters.id as clusterId,
	   semantics_clusters.name as clusterName,
	   site_pages.id as pageId,
	   site_pages.url as pageUrl,
	   site_pages.site_id as siteId,
	   site_sites.url as siteUrl,
	   semantics_core_phrases.frequency as frequency
from (
select
       id,
	   date,
	   position,
	   relevant_url,
	   restrictions_probability,
	   url_changed,
	   identifier_id,
	   phrase_id
from overoptimisation
where identifier_id = 1) current

left join (
select
       id,
	   date,
	   position,
	   relevant_url,
	   restrictions_probability,
	   url_changed,
	   identifier_id,
	   phrase_id
from overoptimisation
where identifier_id = 1) previous

on current.phrase_id = previous.phrase_id
inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
inner join site_pages on semantics_clusters.page_id = site_pages.id
inner join site_sites on site_pages.site_id = site_sites.id

This select works. But I have failed to add sorting.

How can I add order by for these fields?

  1. siteId
  2. pageId
  3. clusterId
  4. frequency

答案1

得分: 0

不见得添加order by子句有什么问题。

基本上你的SQL是有效的,你可以验证它,例如,在这里

如果你在最后添加order by子句,那也是有效的:

select
  current.id as currentId,
  current.date as currentDate,
  current.position as currentPosition,
  current.relevant_url as currentRelevantUrl,
  current.restrictions_probability as currentRestrictionsProbability,
  current.url_changed as currentUrlChanged,
  current.identifier_id as currentIdentifierId,
  current.phrase_id as phraseId,
  previous.id as previousId,
  previous.date as previousDate,
  previous.position as previousPosition,
  previous.relevant_url as previousRelevantUrl,
  previous.restrictions_probability as previousRestrictionsProbability,
  previous.url_changed as previousUrlChanged,
  previous.identifier_id as previousIdentifierId,
  semantics_clusters.id as clusterId,
  semantics_clusters.name as clusterName,
  site_pages.id as pageId,
  site_pages.url as pageUrl,
  site_pages.site_id as siteId,
  site_sites.url as siteUrl,
  semantics_core_phrases.frequency as frequency
from
  (
    select
      id,
      date,
      position,
      relevant_url,
      restrictions_probability,
      url_changed,
      identifier_id,
      phrase_id
    from
      overoptimisation
    where
      identifier_id = 1
  ) current
  left join (
    select
      id,
      date,
      position,
      relevant_url,
      restrictions_probability,
      url_changed,
      identifier_id,
      phrase_id
    from
      overoptimisation
    where
      identifier_id = 1
  ) previous on current.phrase_id = previous.phrase_id
  inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
  inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
  inner join site_pages on semantics_clusters.page_id = site_pages.id
  inner join site_sites on site_pages.site_id = site_sites.id
order by
  siteId

同样,你也可以按pageIdclusterIdfrequency来排序。

英文:

I don't see any problem to add order by clause.

Basically your SQL is valid, you can check it, for example, here

If you add order by clause in the end, then it is valid too:

select
  current.id as currentId,
  current.date as currentDate,
  current.position as currentPosition,
  current.relevant_url as currentRelevantUrl,
  current.restrictions_probability as currentRestrictionsProbability,
  current.url_changed as currentUrlChanged,
  current.identifier_id as currentIdentifierId,
  current.phrase_id as phraseId,
  previous.id as previousId,
  previous.date as previousDate,
  previous.position as previousPosition,
  previous.relevant_url as previousRelevantUrl,
  previous.restrictions_probability as previousRestrictionsProbability,
  previous.url_changed as previousUrlChanged,
  previous.identifier_id as previousIdentifierId,
  semantics_clusters.id as clusterId,
  semantics_clusters.name as clusterName,
  site_pages.id as pageId,
  site_pages.url as pageUrl,
  site_pages.site_id as siteId,
  site_sites.url as siteUrl,
  semantics_core_phrases.frequency as frequency
from
  (
    select
      id,
      date,
      position,
      relevant_url,
      restrictions_probability,
      url_changed,
      identifier_id,
      phrase_id
    from
      overoptimisation
    where
      identifier_id = 1
  ) current
  left join (
    select
      id,
      date,
      position,
      relevant_url,
      restrictions_probability,
      url_changed,
      identifier_id,
      phrase_id
    from
      overoptimisation
    where
      identifier_id = 1
  ) previous on current.phrase_id = previous.phrase_id
  inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
  inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
  inner join site_pages on semantics_clusters.page_id = site_pages.id
  inner join site_sites on site_pages.site_id = site_sites.id
order by
  siteId

The same way you can order by pageId, clusterId, frequency

huangapple
  • 本文由 发表于 2023年2月14日 01:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定