英文:
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
这个查询有效。但我未能添加排序。
我如何为以下字段添加排序?
- siteId
- pageId
- clusterId
- 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?
- siteId
- pageId
- clusterId
- 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
同样,你也可以按pageId
、clusterId
、frequency
来排序。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论