[英]Postgresql order by column and by timestamp
我的桌子上有評論和回復。 我正在使用以下查詢來獲取父級,然后從那里獲取應該與之相關的答復(replyid)。
我面臨的問題是,當我獲取數據時,可以按列(回復)組織數據,但不能按時間戳組織數據。 或者似乎postgresql完全忽略了第二個順序。
parentid | replyid | commentowner | username | commentbody | postcreation
------------------+------------------+---------------+----------+-------------------------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | bob@none.com | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | bob@none.com | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| 3X77KW5t9X43Psfp | bob@none.com | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | bob@none.com | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| BG7mm4cj7Q3Ujffs | bob@none.com | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| 3X77KW5t9X43Psfp | mike@none.com | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | mike@none.com | Mikey | Mike sent this!! | 2018-05-09 08:27:50.1
和用戶表:
username | socialkey | emailaddress | bannerpic | avatarpic
----------+------------------------------------------------------------------+---------------+------------------------------------------------------------------------------+------------------------------------------------------------------------------------
Bobert | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | bob@none.com | https://craftedin.co/static/images/user/headers/59d9bce7c33561.24964635.jpg | https://craftedin.co/static/images/user/avatars/thumbs/59159be40a8c76.26091804.jpg
Mikey | 3bcd6c5f811ba06aa49c2df9504fe5416f35702b78bcdc15ecbd5137fabdca59 | mike@none.com | https://i.pinimg.com/originals/29/85/16/298516524d3ed9eb35ea9aae0f111ab3.jpg | https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg
如果這有助於制作某種Codepen或其他東西,請告訴我,我將盡力做到這一點。
這是我正在運行的查詢:
with parents as (select distinct replyid from comments where commentowner = (select emailaddress from users where SocialKey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a')) and parentid IS NOT NULL LIMIT 5 ), socialkeys as (select distinct socialkey,emailaddress,avatarpic from users) select distinct comments.parentid, comments.replyid,comments.username, comments.commentbody,comments.postcreation from comments left join parents on parents.replyid = comments.replyid left join socialkeys on comments.commentowner = socialkeys.emailaddress where parents.replyid = comments.replyid order by replyid, postcreation;
這是我收到的數據:
parentid | replyid | username | commentbody | postcreation
------------------+------------------+----------+-------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
| 3X77KW5t9X43Psfp | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| 3X77KW5t9X43Psfp | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | Mikey | Mike sent this!! | 2018-05-09 08:27:50.140229
| 3X77KW5t9X43Psfp | Bobert | bob | 2018-06-28 14:22:07.131133
| 3X77KW5t9X43Psfp | Bobert | Oh so this really does work? | 2018-06-28 14:27:02.956613
| 3X77KW5t9X43Psfp | Bobert | this is a reply | 2018-06-28 17:41:49.071165
wpdcBFUft0r597IK | 4WMFSMSTSomjdoD9 | Bobert | this is parent comment number 5 I think the database is set to limit 5... | 2018-06-28 18:15:47.534429
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| BG7mm4cj7Q3Ujffs | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| BG7mm4cj7Q3Ujffs | Bobert | Another lame coment reply | 2018-05-19 12:55:56.090902
pw22kSb7MdA9Nx5G | MD3jA9znknV9c3p7 | Bobert | This is a new comment from bobbert on the new interfce | 2018-06-28 18:03:30.05851
bffV59nEQRg5YtP3 | tIZYNjkyzoHhKfza | Bobert | This is comment # 6... This should show up becasue it is the newest! of the newsts! | 2018-06-28 18:16:12.823547
這是我想收到的。 所有評論都已按replyid收集並排序,但是我希望這些評論也與他們的輸入時間保持一致。 我希望這是有道理的。 我試圖將所有內容都繪制出來。
parentid | replyid | username | commentbody | postcreation
------------------+------------------+----------+-------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
| 3X77KW5t9X43Psfp | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| 3X77KW5t9X43Psfp | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | Mikey | Mike sent this!! | 2018-05-09 08:27:50.140229
| 3X77KW5t9X43Psfp | Bobert | bob | 2018-06-28 14:22:07.131133
| 3X77KW5t9X43Psfp | Bobert | Oh so this really does work? | 2018-06-28 14:27:02.956613
| 3X77KW5t9X43Psfp | Bobert | this is a reply | 2018-06-28 17:41:49.071165
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| BG7mm4cj7Q3Ujffs | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| BG7mm4cj7Q3Ujffs | Bobert | Another lame coment reply | 2018-05-19 12:55:56.090902
pw22kSb7MdA9Nx5G | MD3jA9znknV9c3p7 | Bobert | This is a new comment from bobbert on the new interfce | 2018-06-28 18:03:30.05851
wpdcBFUft0r597IK | 4WMFSMSTSomjdoD9 | Bobert | this is parent comment number 5 I think the database is set to limit 5... | 2018-06-28 18:15:47.534429
bffV59nEQRg5YtP3 | tIZYNjkyzoHhKfza | Bobert | This is comment # 6... This should show up becasue it is the newest! of the newsts! | 2018-06-28 18:16:12.823547
我這樣做是完全錯誤的嗎? 還是我缺少一些簡單的東西?
請讓我知道,謝謝。
根據您的說法,從2個輸出中看來,parentid = wpdcBFUft0r597IK的條目的順序錯誤。 只有一個條目帶有該父ID,因此您的查詢不在乎父ID的顯示順序。如果您希望按父ID的后置排序,請嘗試將查詢更改為以下內容。
with parents
as (select replyid,
min(postcreation) postcreation
from comments
where commentowner = (select emailaddress
from users
where socialkey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a'))
and parentid is not null
group by replyid limit 5),
socialkeys
as (select distinct socialkey,
emailaddress,
avatarpic
from users)
select distinct parents.postcreation,
comments.parentid,
comments.replyid,
comments.username,
comments.commentbody,
comments.postcreation
from comments
left join parents on parents.replyid = comments.replyid
left join socialkeys on comments.commentowner = socialkeys.emailaddress
where parents.replyid = comments.replyid
order by parents.postcreation,
comments.replyid,
comments.postcreation;
我將您的父母CTE更改為分組,以便您可以按訂單使用MIN POSTCREATION。 我還用表名限定了ORDER BY字段,以防在REPLYID排序時出現歧義。
-HTH
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.