英文:
postgresql order by from result of other query
问题
这是我的第一个查询:
SELECT
ss.id AS ss_number
FROM sales_sourcing ss
WHERE ss.state NOT IN ('draft','cancel')
AND ss.source_type = 'import'
这是我的第二个查询:
SELECT write_date FROM sales_sourcing_chat ORDER BY create_date DESC
两个查询之间的关系是 ss.id = chat_id。
我尝试的是根据第二个查询中最新 write_date 的顺序对第一个查询进行排序。
英文:
I have this two query.This is my first query:
SELECT
ss.id ss_number
FROM sales_sourcing ss
WHERE ss.state NOT IN ('draft','cancel')
AND ss.source_type = 'import'
This is my seccond query:
SELECT write_date FROM sales_sourcing_chat ORDER BY create_date DESC
The relation is that the ss.id = chat_id<br>
What I'm trying to do is to order the first query base on the order of the latest write_date from second query.
答案1
得分: 1
根据 Arkena 的建议,看起来您想要加入它们:
SELECT ss.id ss_number
FROM sales_sourcing ss join sales_sourcing_chat ssc
on ss.id = ssc.chat_id
WHERE ss.state NOT IN ('draft','cancel')
AND ss.source_type = 'import'
ORDER BY ssc.create_date desc,
ssc.write_date desc;
您可能需要澄清这是否是您想要的顺序。毕竟,您试图按 write_date
值排序,这些值本身已经按 create_date
排序。
英文:
As suggested by Arkhena, it looks like you want to join them
SELECT ss.id ss_number
FROM sales_sourcing ss join sales_sourcing_chat ssc
on ss.id = ssc.chat_id
WHERE ss.state NOT IN ('draft','cancel')
AND ss.source_type = 'import'
ORDER BY ssc.create_date desc,
ssc.write_date desc;
You might want to clarify if that's the order you aimed for. After all, you're trying to order by write_date
values that are themselves ordered by create_date
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论